sun4i-gpadc-iio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. struct thermal_zone_device *tzd;
  99. struct device *sensor_device;
  100. };
  101. #define SUN4I_GPADC_ADC_CHANNEL(_channel, _name) { \
  102. .type = IIO_VOLTAGE, \
  103. .indexed = 1, \
  104. .channel = _channel, \
  105. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  106. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  107. .datasheet_name = _name, \
  108. }
  109. static struct iio_map sun4i_gpadc_hwmon_maps[] = {
  110. {
  111. .adc_channel_label = "temp_adc",
  112. .consumer_dev_name = "iio_hwmon.0",
  113. },
  114. { /* sentinel */ },
  115. };
  116. static const struct iio_chan_spec sun4i_gpadc_channels[] = {
  117. SUN4I_GPADC_ADC_CHANNEL(0, "adc_chan0"),
  118. SUN4I_GPADC_ADC_CHANNEL(1, "adc_chan1"),
  119. SUN4I_GPADC_ADC_CHANNEL(2, "adc_chan2"),
  120. SUN4I_GPADC_ADC_CHANNEL(3, "adc_chan3"),
  121. {
  122. .type = IIO_TEMP,
  123. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  124. BIT(IIO_CHAN_INFO_SCALE) |
  125. BIT(IIO_CHAN_INFO_OFFSET),
  126. .datasheet_name = "temp_adc",
  127. },
  128. };
  129. static const struct iio_chan_spec sun4i_gpadc_channels_no_temp[] = {
  130. SUN4I_GPADC_ADC_CHANNEL(0, "adc_chan0"),
  131. SUN4I_GPADC_ADC_CHANNEL(1, "adc_chan1"),
  132. SUN4I_GPADC_ADC_CHANNEL(2, "adc_chan2"),
  133. SUN4I_GPADC_ADC_CHANNEL(3, "adc_chan3"),
  134. };
  135. static const struct iio_chan_spec sun8i_a33_gpadc_channels[] = {
  136. {
  137. .type = IIO_TEMP,
  138. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  139. BIT(IIO_CHAN_INFO_SCALE) |
  140. BIT(IIO_CHAN_INFO_OFFSET),
  141. .datasheet_name = "temp_adc",
  142. },
  143. };
  144. static const struct regmap_config sun4i_gpadc_regmap_config = {
  145. .reg_bits = 32,
  146. .val_bits = 32,
  147. .reg_stride = 4,
  148. .fast_io = true,
  149. };
  150. static int sun4i_prepare_for_irq(struct iio_dev *indio_dev, int channel,
  151. unsigned int irq)
  152. {
  153. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  154. int ret;
  155. u32 reg;
  156. pm_runtime_get_sync(indio_dev->dev.parent);
  157. reinit_completion(&info->completion);
  158. ret = regmap_write(info->regmap, SUN4I_GPADC_INT_FIFOC,
  159. SUN4I_GPADC_INT_FIFOC_TP_FIFO_TRIG_LEVEL(1) |
  160. SUN4I_GPADC_INT_FIFOC_TP_FIFO_FLUSH);
  161. if (ret)
  162. return ret;
  163. ret = regmap_read(info->regmap, SUN4I_GPADC_CTRL1, &reg);
  164. if (ret)
  165. return ret;
  166. if (irq == info->fifo_data_irq) {
  167. ret = regmap_write(info->regmap, SUN4I_GPADC_CTRL1,
  168. info->data->tp_mode_en |
  169. info->data->tp_adc_select |
  170. info->data->adc_chan_select(channel));
  171. /*
  172. * When the IP changes channel, it needs a bit of time to get
  173. * correct values.
  174. */
  175. if ((reg & info->data->adc_chan_mask) !=
  176. info->data->adc_chan_select(channel))
  177. mdelay(10);
  178. } else {
  179. /*
  180. * The temperature sensor returns valid data only when the ADC
  181. * operates in touchscreen mode.
  182. */
  183. ret = regmap_write(info->regmap, SUN4I_GPADC_CTRL1,
  184. info->data->tp_mode_en);
  185. }
  186. if (ret)
  187. return ret;
  188. /*
  189. * When the IP changes mode between ADC or touchscreen, it
  190. * needs a bit of time to get correct values.
  191. */
  192. if ((reg & info->data->tp_adc_select) != info->data->tp_adc_select)
  193. mdelay(100);
  194. return 0;
  195. }
  196. static int sun4i_gpadc_read(struct iio_dev *indio_dev, int channel, int *val,
  197. unsigned int irq)
  198. {
  199. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  200. int ret;
  201. mutex_lock(&info->mutex);
  202. ret = sun4i_prepare_for_irq(indio_dev, channel, irq);
  203. if (ret)
  204. goto err;
  205. enable_irq(irq);
  206. /*
  207. * The temperature sensor throws an interruption periodically (currently
  208. * set at periods of ~0.6s in sun4i_gpadc_runtime_resume). A 1s delay
  209. * makes sure an interruption occurs in normal conditions. If it doesn't
  210. * occur, then there is a timeout.
  211. */
  212. if (!wait_for_completion_timeout(&info->completion,
  213. msecs_to_jiffies(1000))) {
  214. ret = -ETIMEDOUT;
  215. goto err;
  216. }
  217. if (irq == info->fifo_data_irq)
  218. *val = info->adc_data;
  219. else
  220. *val = info->temp_data;
  221. ret = 0;
  222. pm_runtime_mark_last_busy(indio_dev->dev.parent);
  223. err:
  224. pm_runtime_put_autosuspend(indio_dev->dev.parent);
  225. disable_irq(irq);
  226. mutex_unlock(&info->mutex);
  227. return ret;
  228. }
  229. static int sun4i_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
  230. int *val)
  231. {
  232. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  233. return sun4i_gpadc_read(indio_dev, channel, val, info->fifo_data_irq);
  234. }
  235. static int sun4i_gpadc_temp_read(struct iio_dev *indio_dev, int *val)
  236. {
  237. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  238. if (info->no_irq) {
  239. pm_runtime_get_sync(indio_dev->dev.parent);
  240. regmap_read(info->regmap, SUN4I_GPADC_TEMP_DATA, val);
  241. pm_runtime_mark_last_busy(indio_dev->dev.parent);
  242. pm_runtime_put_autosuspend(indio_dev->dev.parent);
  243. return 0;
  244. }
  245. return sun4i_gpadc_read(indio_dev, 0, val, info->temp_data_irq);
  246. }
  247. static int sun4i_gpadc_temp_offset(struct iio_dev *indio_dev, int *val)
  248. {
  249. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  250. *val = info->data->temp_offset;
  251. return 0;
  252. }
  253. static int sun4i_gpadc_temp_scale(struct iio_dev *indio_dev, int *val)
  254. {
  255. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  256. *val = info->data->temp_scale;
  257. return 0;
  258. }
  259. static int sun4i_gpadc_read_raw(struct iio_dev *indio_dev,
  260. struct iio_chan_spec const *chan, int *val,
  261. int *val2, long mask)
  262. {
  263. int ret;
  264. switch (mask) {
  265. case IIO_CHAN_INFO_OFFSET:
  266. ret = sun4i_gpadc_temp_offset(indio_dev, val);
  267. if (ret)
  268. return ret;
  269. return IIO_VAL_INT;
  270. case IIO_CHAN_INFO_RAW:
  271. if (chan->type == IIO_VOLTAGE)
  272. ret = sun4i_gpadc_adc_read(indio_dev, chan->channel,
  273. val);
  274. else
  275. ret = sun4i_gpadc_temp_read(indio_dev, val);
  276. if (ret)
  277. return ret;
  278. return IIO_VAL_INT;
  279. case IIO_CHAN_INFO_SCALE:
  280. if (chan->type == IIO_VOLTAGE) {
  281. /* 3000mV / 4096 * raw */
  282. *val = 0;
  283. *val2 = 732421875;
  284. return IIO_VAL_INT_PLUS_NANO;
  285. }
  286. ret = sun4i_gpadc_temp_scale(indio_dev, val);
  287. if (ret)
  288. return ret;
  289. return IIO_VAL_INT;
  290. default:
  291. return -EINVAL;
  292. }
  293. return -EINVAL;
  294. }
  295. static const struct iio_info sun4i_gpadc_iio_info = {
  296. .read_raw = sun4i_gpadc_read_raw,
  297. };
  298. static irqreturn_t sun4i_gpadc_temp_data_irq_handler(int irq, void *dev_id)
  299. {
  300. struct sun4i_gpadc_iio *info = dev_id;
  301. if (atomic_read(&info->ignore_temp_data_irq))
  302. goto out;
  303. if (!regmap_read(info->regmap, SUN4I_GPADC_TEMP_DATA, &info->temp_data))
  304. complete(&info->completion);
  305. out:
  306. return IRQ_HANDLED;
  307. }
  308. static irqreturn_t sun4i_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
  309. {
  310. struct sun4i_gpadc_iio *info = dev_id;
  311. if (atomic_read(&info->ignore_fifo_data_irq))
  312. goto out;
  313. if (!regmap_read(info->regmap, SUN4I_GPADC_DATA, &info->adc_data))
  314. complete(&info->completion);
  315. out:
  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. struct resource *mem;
  417. void __iomem *base;
  418. int ret;
  419. info->data = of_device_get_match_data(&pdev->dev);
  420. if (!info->data)
  421. return -ENODEV;
  422. info->no_irq = true;
  423. indio_dev->num_channels = ARRAY_SIZE(sun8i_a33_gpadc_channels);
  424. indio_dev->channels = sun8i_a33_gpadc_channels;
  425. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  426. base = devm_ioremap_resource(&pdev->dev, mem);
  427. if (IS_ERR(base))
  428. return PTR_ERR(base);
  429. info->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  430. &sun4i_gpadc_regmap_config);
  431. if (IS_ERR(info->regmap)) {
  432. ret = PTR_ERR(info->regmap);
  433. dev_err(&pdev->dev, "failed to init regmap: %d\n", ret);
  434. return ret;
  435. }
  436. if (IS_ENABLED(CONFIG_THERMAL_OF))
  437. info->sensor_device = &pdev->dev;
  438. return 0;
  439. }
  440. static int sun4i_gpadc_probe_mfd(struct platform_device *pdev,
  441. struct iio_dev *indio_dev)
  442. {
  443. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  444. struct sun4i_gpadc_dev *sun4i_gpadc_dev =
  445. dev_get_drvdata(pdev->dev.parent);
  446. int ret;
  447. info->no_irq = false;
  448. info->regmap = sun4i_gpadc_dev->regmap;
  449. indio_dev->num_channels = ARRAY_SIZE(sun4i_gpadc_channels);
  450. indio_dev->channels = sun4i_gpadc_channels;
  451. info->data = (struct gpadc_data *)platform_get_device_id(pdev)->driver_data;
  452. /*
  453. * Since the controller needs to be in touchscreen mode for its thermal
  454. * sensor to operate properly, and that switching between the two modes
  455. * needs a delay, always registering in the thermal framework will
  456. * significantly slow down the conversion rate of the ADCs.
  457. *
  458. * Therefore, instead of depending on THERMAL_OF in Kconfig, we only
  459. * register the sensor if that option is enabled, eventually leaving
  460. * that choice to the user.
  461. */
  462. if (IS_ENABLED(CONFIG_THERMAL_OF)) {
  463. /*
  464. * This driver is a child of an MFD which has a node in the DT
  465. * but not its children, because of DT backward compatibility
  466. * for A10, A13 and A31 SoCs. Therefore, the resulting devices
  467. * of this driver do not have an of_node variable.
  468. * However, its parent (the MFD driver) has an of_node variable
  469. * and since devm_thermal_zone_of_sensor_register uses its first
  470. * argument to match the phandle defined in the node of the
  471. * thermal driver with the of_node of the device passed as first
  472. * argument and the third argument to call ops from
  473. * thermal_zone_of_device_ops, the solution is to use the parent
  474. * device as first argument to match the phandle with its
  475. * of_node, and the device from this driver as third argument to
  476. * return the temperature.
  477. */
  478. info->sensor_device = pdev->dev.parent;
  479. } else {
  480. indio_dev->num_channels =
  481. ARRAY_SIZE(sun4i_gpadc_channels_no_temp);
  482. indio_dev->channels = sun4i_gpadc_channels_no_temp;
  483. }
  484. if (IS_ENABLED(CONFIG_THERMAL_OF)) {
  485. ret = sun4i_irq_init(pdev, "TEMP_DATA_PENDING",
  486. sun4i_gpadc_temp_data_irq_handler,
  487. "temp_data", &info->temp_data_irq,
  488. &info->ignore_temp_data_irq);
  489. if (ret < 0)
  490. return ret;
  491. }
  492. ret = sun4i_irq_init(pdev, "FIFO_DATA_PENDING",
  493. sun4i_gpadc_fifo_data_irq_handler, "fifo_data",
  494. &info->fifo_data_irq, &info->ignore_fifo_data_irq);
  495. if (ret < 0)
  496. return ret;
  497. if (IS_ENABLED(CONFIG_THERMAL_OF)) {
  498. ret = iio_map_array_register(indio_dev, sun4i_gpadc_hwmon_maps);
  499. if (ret < 0) {
  500. dev_err(&pdev->dev,
  501. "failed to register iio map array\n");
  502. return ret;
  503. }
  504. }
  505. return 0;
  506. }
  507. static int sun4i_gpadc_probe(struct platform_device *pdev)
  508. {
  509. struct sun4i_gpadc_iio *info;
  510. struct iio_dev *indio_dev;
  511. int ret;
  512. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
  513. if (!indio_dev)
  514. return -ENOMEM;
  515. info = iio_priv(indio_dev);
  516. platform_set_drvdata(pdev, indio_dev);
  517. mutex_init(&info->mutex);
  518. info->indio_dev = indio_dev;
  519. init_completion(&info->completion);
  520. indio_dev->name = dev_name(&pdev->dev);
  521. indio_dev->dev.parent = &pdev->dev;
  522. indio_dev->dev.of_node = pdev->dev.of_node;
  523. indio_dev->info = &sun4i_gpadc_iio_info;
  524. indio_dev->modes = INDIO_DIRECT_MODE;
  525. if (pdev->dev.of_node)
  526. ret = sun4i_gpadc_probe_dt(pdev, indio_dev);
  527. else
  528. ret = sun4i_gpadc_probe_mfd(pdev, indio_dev);
  529. if (ret)
  530. return ret;
  531. pm_runtime_set_autosuspend_delay(&pdev->dev,
  532. SUN4I_GPADC_AUTOSUSPEND_DELAY);
  533. pm_runtime_use_autosuspend(&pdev->dev);
  534. pm_runtime_set_suspended(&pdev->dev);
  535. pm_runtime_enable(&pdev->dev);
  536. if (IS_ENABLED(CONFIG_THERMAL_OF)) {
  537. info->tzd = thermal_zone_of_sensor_register(info->sensor_device,
  538. 0, info,
  539. &sun4i_ts_tz_ops);
  540. /*
  541. * Do not fail driver probing when failing to register in
  542. * thermal because no thermal DT node is found.
  543. */
  544. if (IS_ERR(info->tzd) && PTR_ERR(info->tzd) != -ENODEV) {
  545. dev_err(&pdev->dev,
  546. "could not register thermal sensor: %ld\n",
  547. PTR_ERR(info->tzd));
  548. return PTR_ERR(info->tzd);
  549. }
  550. }
  551. ret = devm_iio_device_register(&pdev->dev, indio_dev);
  552. if (ret < 0) {
  553. dev_err(&pdev->dev, "could not register the device\n");
  554. goto err_map;
  555. }
  556. return 0;
  557. err_map:
  558. if (!info->no_irq && IS_ENABLED(CONFIG_THERMAL_OF))
  559. iio_map_array_unregister(indio_dev);
  560. pm_runtime_put(&pdev->dev);
  561. pm_runtime_disable(&pdev->dev);
  562. return ret;
  563. }
  564. static int sun4i_gpadc_remove(struct platform_device *pdev)
  565. {
  566. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  567. struct sun4i_gpadc_iio *info = iio_priv(indio_dev);
  568. pm_runtime_put(&pdev->dev);
  569. pm_runtime_disable(&pdev->dev);
  570. if (!IS_ENABLED(CONFIG_THERMAL_OF))
  571. return 0;
  572. thermal_zone_of_sensor_unregister(info->sensor_device, info->tzd);
  573. if (!info->no_irq)
  574. iio_map_array_unregister(indio_dev);
  575. return 0;
  576. }
  577. static const struct platform_device_id sun4i_gpadc_id[] = {
  578. { "sun4i-a10-gpadc-iio", (kernel_ulong_t)&sun4i_gpadc_data },
  579. { "sun5i-a13-gpadc-iio", (kernel_ulong_t)&sun5i_gpadc_data },
  580. { "sun6i-a31-gpadc-iio", (kernel_ulong_t)&sun6i_gpadc_data },
  581. { /* sentinel */ },
  582. };
  583. MODULE_DEVICE_TABLE(platform, sun4i_gpadc_id);
  584. static struct platform_driver sun4i_gpadc_driver = {
  585. .driver = {
  586. .name = "sun4i-gpadc-iio",
  587. .of_match_table = sun4i_gpadc_of_id,
  588. .pm = &sun4i_gpadc_pm_ops,
  589. },
  590. .id_table = sun4i_gpadc_id,
  591. .probe = sun4i_gpadc_probe,
  592. .remove = sun4i_gpadc_remove,
  593. };
  594. MODULE_DEVICE_TABLE(of, sun4i_gpadc_of_id);
  595. module_platform_driver(sun4i_gpadc_driver);
  596. MODULE_DESCRIPTION("ADC driver for sunxi platforms");
  597. MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
  598. MODULE_LICENSE("GPL v2");