rcar-gyroadc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Renesas R-Car GyroADC driver
  4. *
  5. * Copyright 2016 Marek Vasut <marek.vasut@gmail.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/delay.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/io.h>
  13. #include <linux/clk.h>
  14. #include <linux/of.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/err.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/sysfs.h>
  22. #include <linux/iio/trigger.h>
  23. #define DRIVER_NAME "rcar-gyroadc"
  24. /* GyroADC registers. */
  25. #define RCAR_GYROADC_MODE_SELECT 0x00
  26. #define RCAR_GYROADC_MODE_SELECT_1_MB88101A 0x0
  27. #define RCAR_GYROADC_MODE_SELECT_2_ADCS7476 0x1
  28. #define RCAR_GYROADC_MODE_SELECT_3_MAX1162 0x3
  29. #define RCAR_GYROADC_START_STOP 0x04
  30. #define RCAR_GYROADC_START_STOP_START BIT(0)
  31. #define RCAR_GYROADC_CLOCK_LENGTH 0x08
  32. #define RCAR_GYROADC_1_25MS_LENGTH 0x0c
  33. #define RCAR_GYROADC_REALTIME_DATA(ch) (0x10 + ((ch) * 4))
  34. #define RCAR_GYROADC_100MS_ADDED_DATA(ch) (0x30 + ((ch) * 4))
  35. #define RCAR_GYROADC_10MS_AVG_DATA(ch) (0x50 + ((ch) * 4))
  36. #define RCAR_GYROADC_FIFO_STATUS 0x70
  37. #define RCAR_GYROADC_FIFO_STATUS_EMPTY(ch) BIT(0 + (4 * (ch)))
  38. #define RCAR_GYROADC_FIFO_STATUS_FULL(ch) BIT(1 + (4 * (ch)))
  39. #define RCAR_GYROADC_FIFO_STATUS_ERROR(ch) BIT(2 + (4 * (ch)))
  40. #define RCAR_GYROADC_INTR 0x74
  41. #define RCAR_GYROADC_INTR_INT BIT(0)
  42. #define RCAR_GYROADC_INTENR 0x78
  43. #define RCAR_GYROADC_INTENR_INTEN BIT(0)
  44. #define RCAR_GYROADC_SAMPLE_RATE 800 /* Hz */
  45. #define RCAR_GYROADC_RUNTIME_PM_DELAY_MS 2000
  46. enum rcar_gyroadc_model {
  47. RCAR_GYROADC_MODEL_DEFAULT,
  48. RCAR_GYROADC_MODEL_R8A7792,
  49. };
  50. struct rcar_gyroadc {
  51. struct device *dev;
  52. void __iomem *regs;
  53. struct clk *clk;
  54. struct regulator *vref[8];
  55. unsigned int num_channels;
  56. enum rcar_gyroadc_model model;
  57. unsigned int mode;
  58. unsigned int sample_width;
  59. };
  60. static void rcar_gyroadc_hw_init(struct rcar_gyroadc *priv)
  61. {
  62. const unsigned long clk_mhz = clk_get_rate(priv->clk) / 1000000;
  63. const unsigned long clk_mul =
  64. (priv->mode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) ? 10 : 5;
  65. unsigned long clk_len = clk_mhz * clk_mul;
  66. /*
  67. * According to the R-Car Gen2 datasheet Rev. 1.01, Sept 08 2014,
  68. * page 77-7, clock length must be even number. If it's odd number,
  69. * add one.
  70. */
  71. if (clk_len & 1)
  72. clk_len++;
  73. /* Stop the GyroADC. */
  74. writel(0, priv->regs + RCAR_GYROADC_START_STOP);
  75. /* Disable IRQ on V2H. */
  76. if (priv->model == RCAR_GYROADC_MODEL_R8A7792)
  77. writel(0, priv->regs + RCAR_GYROADC_INTENR);
  78. /* Set mode and timing. */
  79. writel(priv->mode, priv->regs + RCAR_GYROADC_MODE_SELECT);
  80. writel(clk_len, priv->regs + RCAR_GYROADC_CLOCK_LENGTH);
  81. writel(clk_mhz * 1250, priv->regs + RCAR_GYROADC_1_25MS_LENGTH);
  82. }
  83. static void rcar_gyroadc_hw_start(struct rcar_gyroadc *priv)
  84. {
  85. /* Start sampling. */
  86. writel(RCAR_GYROADC_START_STOP_START,
  87. priv->regs + RCAR_GYROADC_START_STOP);
  88. /*
  89. * Wait for the first conversion to complete. This is longer than
  90. * the 1.25 mS in the datasheet because 1.25 mS is not enough for
  91. * the hardware to deliver the first sample and the hardware does
  92. * then return zeroes instead of valid data.
  93. */
  94. mdelay(3);
  95. }
  96. static void rcar_gyroadc_hw_stop(struct rcar_gyroadc *priv)
  97. {
  98. /* Stop the GyroADC. */
  99. writel(0, priv->regs + RCAR_GYROADC_START_STOP);
  100. }
  101. #define RCAR_GYROADC_CHAN(_idx) { \
  102. .type = IIO_VOLTAGE, \
  103. .indexed = 1, \
  104. .channel = (_idx), \
  105. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  106. BIT(IIO_CHAN_INFO_SCALE), \
  107. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  108. }
  109. static const struct iio_chan_spec rcar_gyroadc_iio_channels_1[] = {
  110. RCAR_GYROADC_CHAN(0),
  111. RCAR_GYROADC_CHAN(1),
  112. RCAR_GYROADC_CHAN(2),
  113. RCAR_GYROADC_CHAN(3),
  114. };
  115. static const struct iio_chan_spec rcar_gyroadc_iio_channels_2[] = {
  116. RCAR_GYROADC_CHAN(0),
  117. RCAR_GYROADC_CHAN(1),
  118. RCAR_GYROADC_CHAN(2),
  119. RCAR_GYROADC_CHAN(3),
  120. RCAR_GYROADC_CHAN(4),
  121. RCAR_GYROADC_CHAN(5),
  122. RCAR_GYROADC_CHAN(6),
  123. RCAR_GYROADC_CHAN(7),
  124. };
  125. static const struct iio_chan_spec rcar_gyroadc_iio_channels_3[] = {
  126. RCAR_GYROADC_CHAN(0),
  127. RCAR_GYROADC_CHAN(1),
  128. RCAR_GYROADC_CHAN(2),
  129. RCAR_GYROADC_CHAN(3),
  130. RCAR_GYROADC_CHAN(4),
  131. RCAR_GYROADC_CHAN(5),
  132. RCAR_GYROADC_CHAN(6),
  133. RCAR_GYROADC_CHAN(7),
  134. };
  135. static int rcar_gyroadc_set_power(struct rcar_gyroadc *priv, bool on)
  136. {
  137. struct device *dev = priv->dev;
  138. int ret;
  139. if (on) {
  140. ret = pm_runtime_get_sync(dev);
  141. if (ret < 0)
  142. pm_runtime_put_noidle(dev);
  143. } else {
  144. pm_runtime_mark_last_busy(dev);
  145. ret = pm_runtime_put_autosuspend(dev);
  146. }
  147. return ret;
  148. }
  149. static int rcar_gyroadc_read_raw(struct iio_dev *indio_dev,
  150. struct iio_chan_spec const *chan,
  151. int *val, int *val2, long mask)
  152. {
  153. struct rcar_gyroadc *priv = iio_priv(indio_dev);
  154. struct regulator *consumer;
  155. unsigned int datareg = RCAR_GYROADC_REALTIME_DATA(chan->channel);
  156. unsigned int vref;
  157. int ret;
  158. /*
  159. * MB88101 is special in that it has only single regulator for
  160. * all four channels.
  161. */
  162. if (priv->mode == RCAR_GYROADC_MODE_SELECT_1_MB88101A)
  163. consumer = priv->vref[0];
  164. else
  165. consumer = priv->vref[chan->channel];
  166. switch (mask) {
  167. case IIO_CHAN_INFO_RAW:
  168. if (chan->type != IIO_VOLTAGE)
  169. return -EINVAL;
  170. /* Channel not connected. */
  171. if (!consumer)
  172. return -EINVAL;
  173. ret = iio_device_claim_direct_mode(indio_dev);
  174. if (ret)
  175. return ret;
  176. ret = rcar_gyroadc_set_power(priv, true);
  177. if (ret < 0) {
  178. iio_device_release_direct_mode(indio_dev);
  179. return ret;
  180. }
  181. *val = readl(priv->regs + datareg);
  182. *val &= BIT(priv->sample_width) - 1;
  183. ret = rcar_gyroadc_set_power(priv, false);
  184. iio_device_release_direct_mode(indio_dev);
  185. if (ret < 0)
  186. return ret;
  187. return IIO_VAL_INT;
  188. case IIO_CHAN_INFO_SCALE:
  189. /* Channel not connected. */
  190. if (!consumer)
  191. return -EINVAL;
  192. vref = regulator_get_voltage(consumer);
  193. *val = vref / 1000;
  194. *val2 = 1 << priv->sample_width;
  195. return IIO_VAL_FRACTIONAL;
  196. case IIO_CHAN_INFO_SAMP_FREQ:
  197. *val = RCAR_GYROADC_SAMPLE_RATE;
  198. return IIO_VAL_INT;
  199. default:
  200. return -EINVAL;
  201. }
  202. }
  203. static int rcar_gyroadc_reg_access(struct iio_dev *indio_dev,
  204. unsigned int reg, unsigned int writeval,
  205. unsigned int *readval)
  206. {
  207. struct rcar_gyroadc *priv = iio_priv(indio_dev);
  208. unsigned int maxreg = RCAR_GYROADC_FIFO_STATUS;
  209. if (readval == NULL)
  210. return -EINVAL;
  211. if (reg % 4)
  212. return -EINVAL;
  213. /* Handle the V2H case with extra interrupt block. */
  214. if (priv->model == RCAR_GYROADC_MODEL_R8A7792)
  215. maxreg = RCAR_GYROADC_INTENR;
  216. if (reg > maxreg)
  217. return -EINVAL;
  218. *readval = readl(priv->regs + reg);
  219. return 0;
  220. }
  221. static const struct iio_info rcar_gyroadc_iio_info = {
  222. .read_raw = rcar_gyroadc_read_raw,
  223. .debugfs_reg_access = rcar_gyroadc_reg_access,
  224. };
  225. static const struct of_device_id rcar_gyroadc_match[] = {
  226. {
  227. /* R-Car compatible GyroADC */
  228. .compatible = "renesas,rcar-gyroadc",
  229. .data = (void *)RCAR_GYROADC_MODEL_DEFAULT,
  230. }, {
  231. /* R-Car V2H specialty with interrupt registers. */
  232. .compatible = "renesas,r8a7792-gyroadc",
  233. .data = (void *)RCAR_GYROADC_MODEL_R8A7792,
  234. }, {
  235. /* sentinel */
  236. }
  237. };
  238. MODULE_DEVICE_TABLE(of, rcar_gyroadc_match);
  239. static const struct of_device_id rcar_gyroadc_child_match[] = {
  240. /* Mode 1 ADCs */
  241. {
  242. .compatible = "fujitsu,mb88101a",
  243. .data = (void *)RCAR_GYROADC_MODE_SELECT_1_MB88101A,
  244. },
  245. /* Mode 2 ADCs */
  246. {
  247. .compatible = "ti,adcs7476",
  248. .data = (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
  249. }, {
  250. .compatible = "ti,adc121",
  251. .data = (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
  252. }, {
  253. .compatible = "adi,ad7476",
  254. .data = (void *)RCAR_GYROADC_MODE_SELECT_2_ADCS7476,
  255. },
  256. /* Mode 3 ADCs */
  257. {
  258. .compatible = "maxim,max1162",
  259. .data = (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162,
  260. }, {
  261. .compatible = "maxim,max11100",
  262. .data = (void *)RCAR_GYROADC_MODE_SELECT_3_MAX1162,
  263. },
  264. { /* sentinel */ }
  265. };
  266. static int rcar_gyroadc_parse_subdevs(struct iio_dev *indio_dev)
  267. {
  268. const struct of_device_id *of_id;
  269. const struct iio_chan_spec *channels;
  270. struct rcar_gyroadc *priv = iio_priv(indio_dev);
  271. struct device *dev = priv->dev;
  272. struct device_node *np = dev->of_node;
  273. struct device_node *child;
  274. struct regulator *vref;
  275. unsigned int reg;
  276. unsigned int adcmode = -1, childmode;
  277. unsigned int sample_width;
  278. unsigned int num_channels;
  279. int ret, first = 1;
  280. for_each_child_of_node(np, child) {
  281. of_id = of_match_node(rcar_gyroadc_child_match, child);
  282. if (!of_id) {
  283. dev_err(dev, "Ignoring unsupported ADC \"%pOFn\".",
  284. child);
  285. continue;
  286. }
  287. childmode = (uintptr_t)of_id->data;
  288. switch (childmode) {
  289. case RCAR_GYROADC_MODE_SELECT_1_MB88101A:
  290. sample_width = 12;
  291. channels = rcar_gyroadc_iio_channels_1;
  292. num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_1);
  293. break;
  294. case RCAR_GYROADC_MODE_SELECT_2_ADCS7476:
  295. sample_width = 15;
  296. channels = rcar_gyroadc_iio_channels_2;
  297. num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_2);
  298. break;
  299. case RCAR_GYROADC_MODE_SELECT_3_MAX1162:
  300. sample_width = 16;
  301. channels = rcar_gyroadc_iio_channels_3;
  302. num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_3);
  303. break;
  304. default:
  305. goto err_e_inval;
  306. }
  307. /*
  308. * MB88101 is special in that it's only a single chip taking
  309. * up all the CHS lines. Thus, the DT binding is also special
  310. * and has no reg property. If we run into such ADC, handle
  311. * it here.
  312. */
  313. if (childmode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) {
  314. reg = 0;
  315. } else {
  316. ret = of_property_read_u32(child, "reg", &reg);
  317. if (ret) {
  318. dev_err(dev,
  319. "Failed to get child reg property of ADC \"%pOFn\".\n",
  320. child);
  321. goto err_of_node_put;
  322. }
  323. /* Channel number is too high. */
  324. if (reg >= num_channels) {
  325. dev_err(dev,
  326. "Only %i channels supported with %pOFn, but reg = <%i>.\n",
  327. num_channels, child, reg);
  328. goto err_e_inval;
  329. }
  330. }
  331. /* Child node selected different mode than the rest. */
  332. if (!first && (adcmode != childmode)) {
  333. dev_err(dev,
  334. "Channel %i uses different ADC mode than the rest.\n",
  335. reg);
  336. goto err_e_inval;
  337. }
  338. /* Channel is valid, grab the regulator. */
  339. dev->of_node = child;
  340. vref = devm_regulator_get(dev, "vref");
  341. dev->of_node = np;
  342. if (IS_ERR(vref)) {
  343. dev_dbg(dev, "Channel %i 'vref' supply not connected.\n",
  344. reg);
  345. ret = PTR_ERR(vref);
  346. goto err_of_node_put;
  347. }
  348. priv->vref[reg] = vref;
  349. if (!first)
  350. continue;
  351. /* First child node which passed sanity tests. */
  352. adcmode = childmode;
  353. first = 0;
  354. priv->num_channels = num_channels;
  355. priv->mode = childmode;
  356. priv->sample_width = sample_width;
  357. indio_dev->channels = channels;
  358. indio_dev->num_channels = num_channels;
  359. /*
  360. * MB88101 is special and we only have one such device
  361. * attached to the GyroADC at a time, so if we found it,
  362. * we can stop parsing here.
  363. */
  364. if (childmode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) {
  365. of_node_put(child);
  366. break;
  367. }
  368. }
  369. if (first) {
  370. dev_err(dev, "No valid ADC channels found, aborting.\n");
  371. return -EINVAL;
  372. }
  373. return 0;
  374. err_e_inval:
  375. ret = -EINVAL;
  376. err_of_node_put:
  377. of_node_put(child);
  378. return ret;
  379. }
  380. static void rcar_gyroadc_deinit_supplies(struct iio_dev *indio_dev)
  381. {
  382. struct rcar_gyroadc *priv = iio_priv(indio_dev);
  383. unsigned int i;
  384. for (i = 0; i < priv->num_channels; i++) {
  385. if (!priv->vref[i])
  386. continue;
  387. regulator_disable(priv->vref[i]);
  388. }
  389. }
  390. static int rcar_gyroadc_init_supplies(struct iio_dev *indio_dev)
  391. {
  392. struct rcar_gyroadc *priv = iio_priv(indio_dev);
  393. struct device *dev = priv->dev;
  394. unsigned int i;
  395. int ret;
  396. for (i = 0; i < priv->num_channels; i++) {
  397. if (!priv->vref[i])
  398. continue;
  399. ret = regulator_enable(priv->vref[i]);
  400. if (ret) {
  401. dev_err(dev, "Failed to enable regulator %i (ret=%i)\n",
  402. i, ret);
  403. goto err;
  404. }
  405. }
  406. return 0;
  407. err:
  408. rcar_gyroadc_deinit_supplies(indio_dev);
  409. return ret;
  410. }
  411. static int rcar_gyroadc_probe(struct platform_device *pdev)
  412. {
  413. struct device *dev = &pdev->dev;
  414. struct rcar_gyroadc *priv;
  415. struct iio_dev *indio_dev;
  416. int ret;
  417. indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
  418. if (!indio_dev)
  419. return -ENOMEM;
  420. priv = iio_priv(indio_dev);
  421. priv->dev = dev;
  422. priv->regs = devm_platform_ioremap_resource(pdev, 0);
  423. if (IS_ERR(priv->regs))
  424. return PTR_ERR(priv->regs);
  425. priv->clk = devm_clk_get(dev, "fck");
  426. if (IS_ERR(priv->clk))
  427. return dev_err_probe(dev, PTR_ERR(priv->clk),
  428. "Failed to get IF clock\n");
  429. ret = rcar_gyroadc_parse_subdevs(indio_dev);
  430. if (ret)
  431. return ret;
  432. ret = rcar_gyroadc_init_supplies(indio_dev);
  433. if (ret)
  434. return ret;
  435. priv->model = (enum rcar_gyroadc_model)
  436. of_device_get_match_data(&pdev->dev);
  437. platform_set_drvdata(pdev, indio_dev);
  438. indio_dev->name = DRIVER_NAME;
  439. indio_dev->info = &rcar_gyroadc_iio_info;
  440. indio_dev->modes = INDIO_DIRECT_MODE;
  441. ret = clk_prepare_enable(priv->clk);
  442. if (ret) {
  443. dev_err(dev, "Could not prepare or enable the IF clock.\n");
  444. goto err_clk_if_enable;
  445. }
  446. pm_runtime_set_autosuspend_delay(dev, RCAR_GYROADC_RUNTIME_PM_DELAY_MS);
  447. pm_runtime_use_autosuspend(dev);
  448. pm_runtime_enable(dev);
  449. pm_runtime_get_sync(dev);
  450. rcar_gyroadc_hw_init(priv);
  451. rcar_gyroadc_hw_start(priv);
  452. ret = iio_device_register(indio_dev);
  453. if (ret) {
  454. dev_err(dev, "Couldn't register IIO device.\n");
  455. goto err_iio_device_register;
  456. }
  457. pm_runtime_put_sync(dev);
  458. return 0;
  459. err_iio_device_register:
  460. rcar_gyroadc_hw_stop(priv);
  461. pm_runtime_put_sync(dev);
  462. pm_runtime_disable(dev);
  463. pm_runtime_set_suspended(dev);
  464. clk_disable_unprepare(priv->clk);
  465. err_clk_if_enable:
  466. rcar_gyroadc_deinit_supplies(indio_dev);
  467. return ret;
  468. }
  469. static int rcar_gyroadc_remove(struct platform_device *pdev)
  470. {
  471. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  472. struct rcar_gyroadc *priv = iio_priv(indio_dev);
  473. struct device *dev = priv->dev;
  474. iio_device_unregister(indio_dev);
  475. pm_runtime_get_sync(dev);
  476. rcar_gyroadc_hw_stop(priv);
  477. pm_runtime_put_sync(dev);
  478. pm_runtime_disable(dev);
  479. pm_runtime_set_suspended(dev);
  480. clk_disable_unprepare(priv->clk);
  481. rcar_gyroadc_deinit_supplies(indio_dev);
  482. return 0;
  483. }
  484. #if defined(CONFIG_PM)
  485. static int rcar_gyroadc_suspend(struct device *dev)
  486. {
  487. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  488. struct rcar_gyroadc *priv = iio_priv(indio_dev);
  489. rcar_gyroadc_hw_stop(priv);
  490. return 0;
  491. }
  492. static int rcar_gyroadc_resume(struct device *dev)
  493. {
  494. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  495. struct rcar_gyroadc *priv = iio_priv(indio_dev);
  496. rcar_gyroadc_hw_start(priv);
  497. return 0;
  498. }
  499. #endif
  500. static const struct dev_pm_ops rcar_gyroadc_pm_ops = {
  501. SET_RUNTIME_PM_OPS(rcar_gyroadc_suspend, rcar_gyroadc_resume, NULL)
  502. };
  503. static struct platform_driver rcar_gyroadc_driver = {
  504. .probe = rcar_gyroadc_probe,
  505. .remove = rcar_gyroadc_remove,
  506. .driver = {
  507. .name = DRIVER_NAME,
  508. .of_match_table = rcar_gyroadc_match,
  509. .pm = &rcar_gyroadc_pm_ops,
  510. },
  511. };
  512. module_platform_driver(rcar_gyroadc_driver);
  513. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  514. MODULE_DESCRIPTION("Renesas R-Car GyroADC driver");
  515. MODULE_LICENSE("GPL");