sun4i-gpadc-iio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /* ADC driver for sunxi platforms' (A10, A13 and A31) GPADC
  2. *
  3. * Copyright (c) 2016 Quentin Schulz <quentin.schulz@free-electrons.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under
  6. * the terms of the GNU General Public License version 2 as published by the
  7. * Free Software Foundation.
  8. *
  9. * The Allwinner SoCs all have an ADC that can also act as a touchscreen
  10. * controller and a thermal sensor.
  11. * The thermal sensor works only when the ADC acts as a touchscreen controller
  12. * and is configured to throw an interrupt every fixed periods of time (let say
  13. * every X seconds).
  14. * One would be tempted to disable the IP on the hardware side rather than
  15. * disabling interrupts to save some power but that resets the internal clock of
  16. * the IP, resulting in having to wait X seconds every time we want to read the
  17. * value of the thermal sensor.
  18. * This is also the reason of using autosuspend in pm_runtime. If there was no
  19. * autosuspend, the thermal sensor would need X seconds after every
  20. * pm_runtime_get_sync to get a value from the ADC. The autosuspend allows the
  21. * thermal sensor to be requested again in a certain time span before it gets
  22. * shutdown for not being used.
  23. */
  24. #include <linux/completion.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/module.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/regmap.h>
  33. #include <linux/thermal.h>
  34. #include <linux/delay.h>
  35. #include <linux/iio/iio.h>
  36. #include <linux/iio/driver.h>
  37. #include <linux/iio/machine.h>
  38. #include <linux/mfd/sun4i-gpadc.h>
  39. static unsigned int sun4i_gpadc_chan_select(unsigned int chan)
  40. {
  41. return SUN4I_GPADC_CTRL1_ADC_CHAN_SELECT(chan);
  42. }
  43. static unsigned int sun6i_gpadc_chan_select(unsigned int chan)
  44. {
  45. return SUN6I_GPADC_CTRL1_ADC_CHAN_SELECT(chan);
  46. }
  47. struct gpadc_data {
  48. int temp_offset;
  49. int temp_scale;
  50. unsigned int tp_mode_en;
  51. unsigned int tp_adc_select;
  52. unsigned int (*adc_chan_select)(unsigned int chan);
  53. unsigned int adc_chan_mask;
  54. };
  55. static const struct gpadc_data sun4i_gpadc_data = {
  56. .temp_offset = -1932,
  57. .temp_scale = 133,
  58. .tp_mode_en = SUN4I_GPADC_CTRL1_TP_MODE_EN,
  59. .tp_adc_select = SUN4I_GPADC_CTRL1_TP_ADC_SELECT,
  60. .adc_chan_select = &sun4i_gpadc_chan_select,
  61. .adc_chan_mask = SUN4I_GPADC_CTRL1_ADC_CHAN_MASK,
  62. };
  63. static const struct gpadc_data sun5i_gpadc_data = {
  64. .temp_offset = -1447,
  65. .temp_scale = 100,
  66. .tp_mode_en = SUN4I_GPADC_CTRL1_TP_MODE_EN,
  67. .tp_adc_select = SUN4I_GPADC_CTRL1_TP_ADC_SELECT,
  68. .adc_chan_select = &sun4i_gpadc_chan_select,
  69. .adc_chan_mask = SUN4I_GPADC_CTRL1_ADC_CHAN_MASK,
  70. };
  71. static const struct gpadc_data sun6i_gpadc_data = {
  72. .temp_offset = -1623,
  73. .temp_scale = 167,
  74. .tp_mode_en = SUN6I_GPADC_CTRL1_TP_MODE_EN,
  75. .tp_adc_select = SUN6I_GPADC_CTRL1_TP_ADC_SELECT,
  76. .adc_chan_select = &sun6i_gpadc_chan_select,
  77. .adc_chan_mask = SUN6I_GPADC_CTRL1_ADC_CHAN_MASK,
  78. };
  79. static const struct gpadc_data sun8i_a33_gpadc_data = {
  80. .temp_offset = -1662,
  81. .temp_scale = 162,
  82. .tp_mode_en = SUN8I_GPADC_CTRL1_CHOP_TEMP_EN,
  83. };
  84. struct sun4i_gpadc_iio {
  85. struct iio_dev *indio_dev;
  86. struct completion completion;
  87. int temp_data;
  88. u32 adc_data;
  89. struct regmap *regmap;
  90. unsigned int fifo_data_irq;
  91. atomic_t ignore_fifo_data_irq;
  92. unsigned int temp_data_irq;
  93. atomic_t ignore_temp_data_irq;
  94. const struct gpadc_data *data;
  95. bool no_irq;
  96. /* prevents concurrent reads of temperature and ADC */
  97. struct mutex mutex;
  98. };
  99. #define SUN4I_GPADC_ADC_CHANNEL(_channel, _name) { \
  100. .type = IIO_VOLTAGE, \
  101. .indexed = 1, \
  102. .channel = _channel, \
  103. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  104. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  105. .datasheet_name = _name, \
  106. }
  107. static struct iio_map sun4i_gpadc_hwmon_maps[] = {
  108. {
  109. .adc_channel_label = "temp_adc",
  110. .consumer_dev_name = "iio_hwmon.0",
  111. },
  112. { /* sentinel */ },
  113. };
  114. static const struct iio_chan_spec sun4i_gpadc_channels[] = {
  115. SUN4I_GPADC_ADC_CHANNEL(0, "adc_chan0"),
  116. SUN4I_GPADC_ADC_CHANNEL(1, "adc_chan1"),
  117. SUN4I_GPADC_ADC_CHANNEL(2, "adc_chan2"),
  118. SUN4I_GPADC_ADC_CHANNEL(3, "adc_chan3"),
  119. {
  120. .type = IIO_TEMP,
  121. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  122. BIT(IIO_CHAN_INFO_SCALE) |
  123. BIT(IIO_CHAN_INFO_OFFSET),
  124. .datasheet_name = "temp_adc",
  125. },
  126. };
  127. static const struct iio_chan_spec sun4i_gpadc_channels_no_temp[] = {
  128. SUN4I_GPADC_ADC_CHANNEL(0, "adc_chan0"),
  129. SUN4I_GPADC_ADC_CHANNEL(1, "adc_chan1"),
  130. SUN4I_GPADC_ADC_CHANNEL(2, "adc_chan2"),
  131. SUN4I_GPADC_ADC_CHANNEL(3, "adc_chan3"),
  132. };
  133. static const struct iio_chan_spec sun8i_a33_gpadc_channels[] = {
  134. {
  135. .type = IIO_TEMP,
  136. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  137. BIT(IIO_CHAN_INFO_SCALE) |
  138. BIT(IIO_CHAN_INFO_OFFSET),
  139. .datasheet_name = "temp_adc",
  140. },
  141. };
  142. static const struct regmap_config sun4i_gpadc_regmap_config = {
  143. .reg_bits = 32,
  144. .val_bits = 32,
  145. .reg_stride = 4,
  146. .fast_io = true,
  147. };
  148. static int sun4i_prepare_for_irq(struct iio_dev *indio_dev, int channel,
  149. unsigned int irq)
  150. {
  151. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  152. int ret;
  153. u32 reg;
  154. pm_runtime_get_sync(indio_dev->dev.parent);
  155. reinit_completion(&info->completion);
  156. ret = regmap_write(info->regmap, SUN4I_GPADC_INT_FIFOC,
  157. SUN4I_GPADC_INT_FIFOC_TP_FIFO_TRIG_LEVEL(1) |
  158. SUN4I_GPADC_INT_FIFOC_TP_FIFO_FLUSH);
  159. if (ret)
  160. return ret;
  161. ret = regmap_read(info->regmap, SUN4I_GPADC_CTRL1, &reg);
  162. if (ret)
  163. return ret;
  164. if (irq == info->fifo_data_irq) {
  165. ret = regmap_write(info->regmap, SUN4I_GPADC_CTRL1,
  166. info->data->tp_mode_en |
  167. info->data->tp_adc_select |
  168. info->data->adc_chan_select(channel));
  169. /*
  170. * When the IP changes channel, it needs a bit of time to get
  171. * correct values.
  172. */
  173. if ((reg & info->data->adc_chan_mask) !=
  174. info->data->adc_chan_select(channel))
  175. mdelay(10);
  176. } else {
  177. /*
  178. * The temperature sensor returns valid data only when the ADC
  179. * operates in touchscreen mode.
  180. */
  181. ret = regmap_write(info->regmap, SUN4I_GPADC_CTRL1,
  182. info->data->tp_mode_en);
  183. }
  184. if (ret)
  185. return ret;
  186. /*
  187. * When the IP changes mode between ADC or touchscreen, it
  188. * needs a bit of time to get correct values.
  189. */
  190. if ((reg & info->data->tp_adc_select) != info->data->tp_adc_select)
  191. mdelay(100);
  192. return 0;
  193. }
  194. static int sun4i_gpadc_read(struct iio_dev *indio_dev, int channel, int *val,
  195. unsigned int irq)
  196. {
  197. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  198. int ret;
  199. mutex_lock(&info->mutex);
  200. ret = sun4i_prepare_for_irq(indio_dev, channel, irq);
  201. if (ret)
  202. goto err;
  203. enable_irq(irq);
  204. /*
  205. * The temperature sensor throws an interruption periodically (currently
  206. * set at periods of ~0.6s in sun4i_gpadc_runtime_resume). A 1s delay
  207. * makes sure an interruption occurs in normal conditions. If it doesn't
  208. * occur, then there is a timeout.
  209. */
  210. if (!wait_for_completion_timeout(&info->completion,
  211. msecs_to_jiffies(1000))) {
  212. ret = -ETIMEDOUT;
  213. goto err;
  214. }
  215. if (irq == info->fifo_data_irq)
  216. *val = info->adc_data;
  217. else
  218. *val = info->temp_data;
  219. ret = 0;
  220. pm_runtime_mark_last_busy(indio_dev->dev.parent);
  221. err:
  222. pm_runtime_put_autosuspend(indio_dev->dev.parent);
  223. mutex_unlock(&info->mutex);
  224. return ret;
  225. }
  226. static int sun4i_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
  227. int *val)
  228. {
  229. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  230. return sun4i_gpadc_read(indio_dev, channel, val, info->fifo_data_irq);
  231. }
  232. static int sun4i_gpadc_temp_read(struct iio_dev *indio_dev, int *val)
  233. {
  234. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  235. if (info->no_irq) {
  236. pm_runtime_get_sync(indio_dev->dev.parent);
  237. regmap_read(info->regmap, SUN4I_GPADC_TEMP_DATA, val);
  238. pm_runtime_mark_last_busy(indio_dev->dev.parent);
  239. pm_runtime_put_autosuspend(indio_dev->dev.parent);
  240. return 0;
  241. }
  242. return sun4i_gpadc_read(indio_dev, 0, val, info->temp_data_irq);
  243. }
  244. static int sun4i_gpadc_temp_offset(struct iio_dev *indio_dev, int *val)
  245. {
  246. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  247. *val = info->data->temp_offset;
  248. return 0;
  249. }
  250. static int sun4i_gpadc_temp_scale(struct iio_dev *indio_dev, int *val)
  251. {
  252. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  253. *val = info->data->temp_scale;
  254. return 0;
  255. }
  256. static int sun4i_gpadc_read_raw(struct iio_dev *indio_dev,
  257. struct iio_chan_spec const *chan, int *val,
  258. int *val2, long mask)
  259. {
  260. int ret;
  261. switch (mask) {
  262. case IIO_CHAN_INFO_OFFSET:
  263. ret = sun4i_gpadc_temp_offset(indio_dev, val);
  264. if (ret)
  265. return ret;
  266. return IIO_VAL_INT;
  267. case IIO_CHAN_INFO_RAW:
  268. if (chan->type == IIO_VOLTAGE)
  269. ret = sun4i_gpadc_adc_read(indio_dev, chan->channel,
  270. val);
  271. else
  272. ret = sun4i_gpadc_temp_read(indio_dev, val);
  273. if (ret)
  274. return ret;
  275. return IIO_VAL_INT;
  276. case IIO_CHAN_INFO_SCALE:
  277. if (chan->type == IIO_VOLTAGE) {
  278. /* 3000mV / 4096 * raw */
  279. *val = 0;
  280. *val2 = 732421875;
  281. return IIO_VAL_INT_PLUS_NANO;
  282. }
  283. ret = sun4i_gpadc_temp_scale(indio_dev, val);
  284. if (ret)
  285. return ret;
  286. return IIO_VAL_INT;
  287. default:
  288. return -EINVAL;
  289. }
  290. return -EINVAL;
  291. }
  292. static const struct iio_info sun4i_gpadc_iio_info = {
  293. .read_raw = sun4i_gpadc_read_raw,
  294. .driver_module = THIS_MODULE,
  295. };
  296. static irqreturn_t sun4i_gpadc_temp_data_irq_handler(int irq, void *dev_id)
  297. {
  298. struct sun4i_gpadc_iio *info = dev_id;
  299. if (atomic_read(&info->ignore_temp_data_irq))
  300. goto out;
  301. if (!regmap_read(info->regmap, SUN4I_GPADC_TEMP_DATA, &info->temp_data))
  302. complete(&info->completion);
  303. out:
  304. disable_irq_nosync(info->temp_data_irq);
  305. return IRQ_HANDLED;
  306. }
  307. static irqreturn_t sun4i_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
  308. {
  309. struct sun4i_gpadc_iio *info = dev_id;
  310. if (atomic_read(&info->ignore_fifo_data_irq))
  311. goto out;
  312. if (!regmap_read(info->regmap, SUN4I_GPADC_DATA, &info->adc_data))
  313. complete(&info->completion);
  314. out:
  315. disable_irq_nosync(info->fifo_data_irq);
  316. return IRQ_HANDLED;
  317. }
  318. static int sun4i_gpadc_runtime_suspend(struct device *dev)
  319. {
  320. struct sun4i_gpadc_iio *info = iio_priv(dev_get_drvdata(dev));
  321. /* Disable the ADC on IP */
  322. regmap_write(info->regmap, SUN4I_GPADC_CTRL1, 0);
  323. /* Disable temperature sensor on IP */
  324. regmap_write(info->regmap, SUN4I_GPADC_TPR, 0);
  325. return 0;
  326. }
  327. static int sun4i_gpadc_runtime_resume(struct device *dev)
  328. {
  329. struct sun4i_gpadc_iio *info = iio_priv(dev_get_drvdata(dev));
  330. /* clkin = 6MHz */
  331. regmap_write(info->regmap, SUN4I_GPADC_CTRL0,
  332. SUN4I_GPADC_CTRL0_ADC_CLK_DIVIDER(2) |
  333. SUN4I_GPADC_CTRL0_FS_DIV(7) |
  334. SUN4I_GPADC_CTRL0_T_ACQ(63));
  335. regmap_write(info->regmap, SUN4I_GPADC_CTRL1, info->data->tp_mode_en);
  336. regmap_write(info->regmap, SUN4I_GPADC_CTRL3,
  337. SUN4I_GPADC_CTRL3_FILTER_EN |
  338. SUN4I_GPADC_CTRL3_FILTER_TYPE(1));
  339. /* period = SUN4I_GPADC_TPR_TEMP_PERIOD * 256 * 16 / clkin; ~0.6s */
  340. regmap_write(info->regmap, SUN4I_GPADC_TPR,
  341. SUN4I_GPADC_TPR_TEMP_ENABLE |
  342. SUN4I_GPADC_TPR_TEMP_PERIOD(800));
  343. return 0;
  344. }
  345. static int sun4i_gpadc_get_temp(void *data, int *temp)
  346. {
  347. struct sun4i_gpadc_iio *info = data;
  348. int val, scale, offset;
  349. if (sun4i_gpadc_temp_read(info->indio_dev, &val))
  350. return -ETIMEDOUT;
  351. sun4i_gpadc_temp_scale(info->indio_dev, &scale);
  352. sun4i_gpadc_temp_offset(info->indio_dev, &offset);
  353. *temp = (val + offset) * scale;
  354. return 0;
  355. }
  356. static const struct thermal_zone_of_device_ops sun4i_ts_tz_ops = {
  357. .get_temp = &sun4i_gpadc_get_temp,
  358. };
  359. static const struct dev_pm_ops sun4i_gpadc_pm_ops = {
  360. .runtime_suspend = &sun4i_gpadc_runtime_suspend,
  361. .runtime_resume = &sun4i_gpadc_runtime_resume,
  362. };
  363. static int sun4i_irq_init(struct platform_device *pdev, const char *name,
  364. irq_handler_t handler, const char *devname,
  365. unsigned int *irq, atomic_t *atomic)
  366. {
  367. int ret;
  368. struct sun4i_gpadc_dev *mfd_dev = dev_get_drvdata(pdev->dev.parent);
  369. struct sun4i_gpadc_iio *info = iio_priv(dev_get_drvdata(&pdev->dev));
  370. /*
  371. * Once the interrupt is activated, the IP continuously performs
  372. * conversions thus throws interrupts. The interrupt is activated right
  373. * after being requested but we want to control when these interrupts
  374. * occur thus we disable it right after being requested. However, an
  375. * interrupt might occur between these two instructions and we have to
  376. * make sure that does not happen, by using atomic flags. We set the
  377. * flag before requesting the interrupt and unset it right after
  378. * disabling the interrupt. When an interrupt occurs between these two
  379. * instructions, reading the atomic flag will tell us to ignore the
  380. * interrupt.
  381. */
  382. atomic_set(atomic, 1);
  383. ret = platform_get_irq_byname(pdev, name);
  384. if (ret < 0) {
  385. dev_err(&pdev->dev, "no %s interrupt registered\n", name);
  386. return ret;
  387. }
  388. ret = regmap_irq_get_virq(mfd_dev->regmap_irqc, ret);
  389. if (ret < 0) {
  390. dev_err(&pdev->dev, "failed to get virq for irq %s\n", name);
  391. return ret;
  392. }
  393. *irq = ret;
  394. ret = devm_request_any_context_irq(&pdev->dev, *irq, handler, 0,
  395. devname, info);
  396. if (ret < 0) {
  397. dev_err(&pdev->dev, "could not request %s interrupt: %d\n",
  398. name, ret);
  399. return ret;
  400. }
  401. disable_irq(*irq);
  402. atomic_set(atomic, 0);
  403. return 0;
  404. }
  405. static const struct of_device_id sun4i_gpadc_of_id[] = {
  406. {
  407. .compatible = "allwinner,sun8i-a33-ths",
  408. .data = &sun8i_a33_gpadc_data,
  409. },
  410. { /* sentinel */ }
  411. };
  412. static int sun4i_gpadc_probe_dt(struct platform_device *pdev,
  413. struct iio_dev *indio_dev)
  414. {
  415. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  416. const struct of_device_id *of_dev;
  417. struct thermal_zone_device *tzd;
  418. struct resource *mem;
  419. void __iomem *base;
  420. int ret;
  421. of_dev = of_match_device(sun4i_gpadc_of_id, &pdev->dev);
  422. if (!of_dev)
  423. return -ENODEV;
  424. info->no_irq = true;
  425. info->data = (struct gpadc_data *)of_dev->data;
  426. indio_dev->num_channels = ARRAY_SIZE(sun8i_a33_gpadc_channels);
  427. indio_dev->channels = sun8i_a33_gpadc_channels;
  428. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  429. base = devm_ioremap_resource(&pdev->dev, mem);
  430. if (IS_ERR(base))
  431. return PTR_ERR(base);
  432. info->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  433. &sun4i_gpadc_regmap_config);
  434. if (IS_ERR(info->regmap)) {
  435. ret = PTR_ERR(info->regmap);
  436. dev_err(&pdev->dev, "failed to init regmap: %d\n", ret);
  437. return ret;
  438. }
  439. if (!IS_ENABLED(CONFIG_THERMAL_OF))
  440. return 0;
  441. tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, info,
  442. &sun4i_ts_tz_ops);
  443. if (IS_ERR(tzd))
  444. dev_err(&pdev->dev, "could not register thermal sensor: %ld\n",
  445. PTR_ERR(tzd));
  446. return PTR_ERR_OR_ZERO(tzd);
  447. }
  448. static int sun4i_gpadc_probe_mfd(struct platform_device *pdev,
  449. struct iio_dev *indio_dev)
  450. {
  451. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  452. struct sun4i_gpadc_dev *sun4i_gpadc_dev =
  453. dev_get_drvdata(pdev->dev.parent);
  454. int ret;
  455. info->no_irq = false;
  456. info->regmap = sun4i_gpadc_dev->regmap;
  457. indio_dev->num_channels = ARRAY_SIZE(sun4i_gpadc_channels);
  458. indio_dev->channels = sun4i_gpadc_channels;
  459. info->data = (struct gpadc_data *)platform_get_device_id(pdev)->driver_data;
  460. /*
  461. * Since the controller needs to be in touchscreen mode for its thermal
  462. * sensor to operate properly, and that switching between the two modes
  463. * needs a delay, always registering in the thermal framework will
  464. * significantly slow down the conversion rate of the ADCs.
  465. *
  466. * Therefore, instead of depending on THERMAL_OF in Kconfig, we only
  467. * register the sensor if that option is enabled, eventually leaving
  468. * that choice to the user.
  469. */
  470. if (IS_ENABLED(CONFIG_THERMAL_OF)) {
  471. /*
  472. * This driver is a child of an MFD which has a node in the DT
  473. * but not its children, because of DT backward compatibility
  474. * for A10, A13 and A31 SoCs. Therefore, the resulting devices
  475. * of this driver do not have an of_node variable.
  476. * However, its parent (the MFD driver) has an of_node variable
  477. * and since devm_thermal_zone_of_sensor_register uses its first
  478. * argument to match the phandle defined in the node of the
  479. * thermal driver with the of_node of the device passed as first
  480. * argument and the third argument to call ops from
  481. * thermal_zone_of_device_ops, the solution is to use the parent
  482. * device as first argument to match the phandle with its
  483. * of_node, and the device from this driver as third argument to
  484. * return the temperature.
  485. */
  486. struct thermal_zone_device *tzd;
  487. tzd = devm_thermal_zone_of_sensor_register(pdev->dev.parent, 0,
  488. info,
  489. &sun4i_ts_tz_ops);
  490. if (IS_ERR(tzd)) {
  491. dev_err(&pdev->dev,
  492. "could not register thermal sensor: %ld\n",
  493. PTR_ERR(tzd));
  494. return PTR_ERR(tzd);
  495. }
  496. } else {
  497. indio_dev->num_channels =
  498. ARRAY_SIZE(sun4i_gpadc_channels_no_temp);
  499. indio_dev->channels = sun4i_gpadc_channels_no_temp;
  500. }
  501. if (IS_ENABLED(CONFIG_THERMAL_OF)) {
  502. ret = sun4i_irq_init(pdev, "TEMP_DATA_PENDING",
  503. sun4i_gpadc_temp_data_irq_handler,
  504. "temp_data", &info->temp_data_irq,
  505. &info->ignore_temp_data_irq);
  506. if (ret < 0)
  507. return ret;
  508. }
  509. ret = sun4i_irq_init(pdev, "FIFO_DATA_PENDING",
  510. sun4i_gpadc_fifo_data_irq_handler, "fifo_data",
  511. &info->fifo_data_irq, &info->ignore_fifo_data_irq);
  512. if (ret < 0)
  513. return ret;
  514. if (IS_ENABLED(CONFIG_THERMAL_OF)) {
  515. ret = iio_map_array_register(indio_dev, sun4i_gpadc_hwmon_maps);
  516. if (ret < 0) {
  517. dev_err(&pdev->dev,
  518. "failed to register iio map array\n");
  519. return ret;
  520. }
  521. }
  522. return 0;
  523. }
  524. static int sun4i_gpadc_probe(struct platform_device *pdev)
  525. {
  526. struct sun4i_gpadc_iio *info;
  527. struct iio_dev *indio_dev;
  528. int ret;
  529. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
  530. if (!indio_dev)
  531. return -ENOMEM;
  532. info = iio_priv(indio_dev);
  533. platform_set_drvdata(pdev, indio_dev);
  534. mutex_init(&info->mutex);
  535. info->indio_dev = indio_dev;
  536. init_completion(&info->completion);
  537. indio_dev->name = dev_name(&pdev->dev);
  538. indio_dev->dev.parent = &pdev->dev;
  539. indio_dev->dev.of_node = pdev->dev.of_node;
  540. indio_dev->info = &sun4i_gpadc_iio_info;
  541. indio_dev->modes = INDIO_DIRECT_MODE;
  542. if (pdev->dev.of_node)
  543. ret = sun4i_gpadc_probe_dt(pdev, indio_dev);
  544. else
  545. ret = sun4i_gpadc_probe_mfd(pdev, indio_dev);
  546. if (ret)
  547. return ret;
  548. pm_runtime_set_autosuspend_delay(&pdev->dev,
  549. SUN4I_GPADC_AUTOSUSPEND_DELAY);
  550. pm_runtime_use_autosuspend(&pdev->dev);
  551. pm_runtime_set_suspended(&pdev->dev);
  552. pm_runtime_enable(&pdev->dev);
  553. ret = devm_iio_device_register(&pdev->dev, indio_dev);
  554. if (ret < 0) {
  555. dev_err(&pdev->dev, "could not register the device\n");
  556. goto err_map;
  557. }
  558. return 0;
  559. err_map:
  560. if (!info->no_irq && IS_ENABLED(CONFIG_THERMAL_OF))
  561. iio_map_array_unregister(indio_dev);
  562. pm_runtime_put(&pdev->dev);
  563. pm_runtime_disable(&pdev->dev);
  564. return ret;
  565. }
  566. static int sun4i_gpadc_remove(struct platform_device *pdev)
  567. {
  568. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  569. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  570. pm_runtime_put(&pdev->dev);
  571. pm_runtime_disable(&pdev->dev);
  572. if (!info->no_irq && IS_ENABLED(CONFIG_THERMAL_OF))
  573. iio_map_array_unregister(indio_dev);
  574. return 0;
  575. }
  576. static const struct platform_device_id sun4i_gpadc_id[] = {
  577. { "sun4i-a10-gpadc-iio", (kernel_ulong_t)&sun4i_gpadc_data },
  578. { "sun5i-a13-gpadc-iio", (kernel_ulong_t)&sun5i_gpadc_data },
  579. { "sun6i-a31-gpadc-iio", (kernel_ulong_t)&sun6i_gpadc_data },
  580. { /* sentinel */ },
  581. };
  582. static struct platform_driver sun4i_gpadc_driver = {
  583. .driver = {
  584. .name = "sun4i-gpadc-iio",
  585. .of_match_table = sun4i_gpadc_of_id,
  586. .pm = &sun4i_gpadc_pm_ops,
  587. },
  588. .id_table = sun4i_gpadc_id,
  589. .probe = sun4i_gpadc_probe,
  590. .remove = sun4i_gpadc_remove,
  591. };
  592. module_platform_driver(sun4i_gpadc_driver);
  593. MODULE_DESCRIPTION("ADC driver for sunxi platforms");
  594. MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
  595. MODULE_LICENSE("GPL v2");