sc27xx_adc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Spreadtrum Communications Inc.
  3. #include <linux/hwspinlock.h>
  4. #include <linux/iio/iio.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/module.h>
  7. #include <linux/of.h>
  8. #include <linux/of_device.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/regmap.h>
  11. /* PMIC global registers definition */
  12. #define SC27XX_MODULE_EN 0xc08
  13. #define SC27XX_MODULE_ADC_EN BIT(5)
  14. #define SC27XX_ARM_CLK_EN 0xc10
  15. #define SC27XX_CLK_ADC_EN BIT(5)
  16. #define SC27XX_CLK_ADC_CLK_EN BIT(6)
  17. /* ADC controller registers definition */
  18. #define SC27XX_ADC_CTL 0x0
  19. #define SC27XX_ADC_CH_CFG 0x4
  20. #define SC27XX_ADC_DATA 0x4c
  21. #define SC27XX_ADC_INT_EN 0x50
  22. #define SC27XX_ADC_INT_CLR 0x54
  23. #define SC27XX_ADC_INT_STS 0x58
  24. #define SC27XX_ADC_INT_RAW 0x5c
  25. /* Bits and mask definition for SC27XX_ADC_CTL register */
  26. #define SC27XX_ADC_EN BIT(0)
  27. #define SC27XX_ADC_CHN_RUN BIT(1)
  28. #define SC27XX_ADC_12BIT_MODE BIT(2)
  29. #define SC27XX_ADC_RUN_NUM_MASK GENMASK(7, 4)
  30. #define SC27XX_ADC_RUN_NUM_SHIFT 4
  31. /* Bits and mask definition for SC27XX_ADC_CH_CFG register */
  32. #define SC27XX_ADC_CHN_ID_MASK GENMASK(4, 0)
  33. #define SC27XX_ADC_SCALE_MASK GENMASK(10, 8)
  34. #define SC27XX_ADC_SCALE_SHIFT 8
  35. /* Bits definitions for SC27XX_ADC_INT_EN registers */
  36. #define SC27XX_ADC_IRQ_EN BIT(0)
  37. /* Bits definitions for SC27XX_ADC_INT_CLR registers */
  38. #define SC27XX_ADC_IRQ_CLR BIT(0)
  39. /* Mask definition for SC27XX_ADC_DATA register */
  40. #define SC27XX_ADC_DATA_MASK GENMASK(11, 0)
  41. /* Timeout (ms) for the trylock of hardware spinlocks */
  42. #define SC27XX_ADC_HWLOCK_TIMEOUT 5000
  43. /* Maximum ADC channel number */
  44. #define SC27XX_ADC_CHANNEL_MAX 32
  45. /* ADC voltage ratio definition */
  46. #define SC27XX_VOLT_RATIO(n, d) \
  47. (((n) << SC27XX_RATIO_NUMERATOR_OFFSET) | (d))
  48. #define SC27XX_RATIO_NUMERATOR_OFFSET 16
  49. #define SC27XX_RATIO_DENOMINATOR_MASK GENMASK(15, 0)
  50. struct sc27xx_adc_data {
  51. struct device *dev;
  52. struct regmap *regmap;
  53. /*
  54. * One hardware spinlock to synchronize between the multiple
  55. * subsystems which will access the unique ADC controller.
  56. */
  57. struct hwspinlock *hwlock;
  58. struct completion completion;
  59. int channel_scale[SC27XX_ADC_CHANNEL_MAX];
  60. u32 base;
  61. int value;
  62. int irq;
  63. };
  64. struct sc27xx_adc_linear_graph {
  65. int volt0;
  66. int adc0;
  67. int volt1;
  68. int adc1;
  69. };
  70. /*
  71. * According to the datasheet, we can convert one ADC value to one voltage value
  72. * through 2 points in the linear graph. If the voltage is less than 1.2v, we
  73. * should use the small-scale graph, and if more than 1.2v, we should use the
  74. * big-scale graph.
  75. */
  76. static const struct sc27xx_adc_linear_graph big_scale_graph = {
  77. 4200, 3310,
  78. 3600, 2832,
  79. };
  80. static const struct sc27xx_adc_linear_graph small_scale_graph = {
  81. 1000, 3413,
  82. 100, 341,
  83. };
  84. static int sc27xx_adc_get_ratio(int channel, int scale)
  85. {
  86. switch (channel) {
  87. case 1:
  88. case 2:
  89. case 3:
  90. case 4:
  91. return scale ? SC27XX_VOLT_RATIO(400, 1025) :
  92. SC27XX_VOLT_RATIO(1, 1);
  93. case 5:
  94. return SC27XX_VOLT_RATIO(7, 29);
  95. case 6:
  96. return SC27XX_VOLT_RATIO(375, 9000);
  97. case 7:
  98. case 8:
  99. return scale ? SC27XX_VOLT_RATIO(100, 125) :
  100. SC27XX_VOLT_RATIO(1, 1);
  101. case 19:
  102. return SC27XX_VOLT_RATIO(1, 3);
  103. default:
  104. return SC27XX_VOLT_RATIO(1, 1);
  105. }
  106. return SC27XX_VOLT_RATIO(1, 1);
  107. }
  108. static int sc27xx_adc_read(struct sc27xx_adc_data *data, int channel,
  109. int scale, int *val)
  110. {
  111. int ret;
  112. u32 tmp;
  113. reinit_completion(&data->completion);
  114. ret = hwspin_lock_timeout_raw(data->hwlock, SC27XX_ADC_HWLOCK_TIMEOUT);
  115. if (ret) {
  116. dev_err(data->dev, "timeout to get the hwspinlock\n");
  117. return ret;
  118. }
  119. ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_CTL,
  120. SC27XX_ADC_EN, SC27XX_ADC_EN);
  121. if (ret)
  122. goto unlock_adc;
  123. /* Configure the channel id and scale */
  124. tmp = (scale << SC27XX_ADC_SCALE_SHIFT) & SC27XX_ADC_SCALE_MASK;
  125. tmp |= channel & SC27XX_ADC_CHN_ID_MASK;
  126. ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_CH_CFG,
  127. SC27XX_ADC_CHN_ID_MASK | SC27XX_ADC_SCALE_MASK,
  128. tmp);
  129. if (ret)
  130. goto disable_adc;
  131. /* Select 12bit conversion mode, and only sample 1 time */
  132. tmp = SC27XX_ADC_12BIT_MODE;
  133. tmp |= (0 << SC27XX_ADC_RUN_NUM_SHIFT) & SC27XX_ADC_RUN_NUM_MASK;
  134. ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_CTL,
  135. SC27XX_ADC_RUN_NUM_MASK | SC27XX_ADC_12BIT_MODE,
  136. tmp);
  137. if (ret)
  138. goto disable_adc;
  139. ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_CTL,
  140. SC27XX_ADC_CHN_RUN, SC27XX_ADC_CHN_RUN);
  141. if (ret)
  142. goto disable_adc;
  143. wait_for_completion(&data->completion);
  144. disable_adc:
  145. regmap_update_bits(data->regmap, data->base + SC27XX_ADC_CTL,
  146. SC27XX_ADC_EN, 0);
  147. unlock_adc:
  148. hwspin_unlock_raw(data->hwlock);
  149. if (!ret)
  150. *val = data->value;
  151. return ret;
  152. }
  153. static irqreturn_t sc27xx_adc_isr(int irq, void *dev_id)
  154. {
  155. struct sc27xx_adc_data *data = dev_id;
  156. int ret;
  157. ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_INT_CLR,
  158. SC27XX_ADC_IRQ_CLR, SC27XX_ADC_IRQ_CLR);
  159. if (ret)
  160. return IRQ_RETVAL(ret);
  161. ret = regmap_read(data->regmap, data->base + SC27XX_ADC_DATA,
  162. &data->value);
  163. if (ret)
  164. return IRQ_RETVAL(ret);
  165. data->value &= SC27XX_ADC_DATA_MASK;
  166. complete(&data->completion);
  167. return IRQ_HANDLED;
  168. }
  169. static void sc27xx_adc_volt_ratio(struct sc27xx_adc_data *data,
  170. int channel, int scale,
  171. u32 *div_numerator, u32 *div_denominator)
  172. {
  173. u32 ratio = sc27xx_adc_get_ratio(channel, scale);
  174. *div_numerator = ratio >> SC27XX_RATIO_NUMERATOR_OFFSET;
  175. *div_denominator = ratio & SC27XX_RATIO_DENOMINATOR_MASK;
  176. }
  177. static int sc27xx_adc_to_volt(const struct sc27xx_adc_linear_graph *graph,
  178. int raw_adc)
  179. {
  180. int tmp;
  181. tmp = (graph->volt0 - graph->volt1) * (raw_adc - graph->adc1);
  182. tmp /= (graph->adc0 - graph->adc1);
  183. tmp += graph->volt1;
  184. return tmp < 0 ? 0 : tmp;
  185. }
  186. static int sc27xx_adc_convert_volt(struct sc27xx_adc_data *data, int channel,
  187. int scale, int raw_adc)
  188. {
  189. u32 numerator, denominator;
  190. u32 volt;
  191. /*
  192. * Convert ADC values to voltage values according to the linear graph,
  193. * and channel 5 and channel 1 has been calibrated, so we can just
  194. * return the voltage values calculated by the linear graph. But other
  195. * channels need be calculated to the real voltage values with the
  196. * voltage ratio.
  197. */
  198. switch (channel) {
  199. case 5:
  200. return sc27xx_adc_to_volt(&big_scale_graph, raw_adc);
  201. case 1:
  202. return sc27xx_adc_to_volt(&small_scale_graph, raw_adc);
  203. default:
  204. volt = sc27xx_adc_to_volt(&small_scale_graph, raw_adc);
  205. break;
  206. }
  207. sc27xx_adc_volt_ratio(data, channel, scale, &numerator, &denominator);
  208. return (volt * denominator + numerator / 2) / numerator;
  209. }
  210. static int sc27xx_adc_read_processed(struct sc27xx_adc_data *data,
  211. int channel, int scale, int *val)
  212. {
  213. int ret, raw_adc;
  214. ret = sc27xx_adc_read(data, channel, scale, &raw_adc);
  215. if (ret)
  216. return ret;
  217. *val = sc27xx_adc_convert_volt(data, channel, scale, raw_adc);
  218. return 0;
  219. }
  220. static int sc27xx_adc_read_raw(struct iio_dev *indio_dev,
  221. struct iio_chan_spec const *chan,
  222. int *val, int *val2, long mask)
  223. {
  224. struct sc27xx_adc_data *data = iio_priv(indio_dev);
  225. int scale = data->channel_scale[chan->channel];
  226. int ret, tmp;
  227. switch (mask) {
  228. case IIO_CHAN_INFO_PROCESSED:
  229. mutex_lock(&indio_dev->mlock);
  230. ret = sc27xx_adc_read_processed(data, chan->channel, scale,
  231. &tmp);
  232. mutex_unlock(&indio_dev->mlock);
  233. if (ret)
  234. return ret;
  235. *val = tmp;
  236. return IIO_VAL_INT;
  237. case IIO_CHAN_INFO_SCALE:
  238. *val = scale;
  239. return IIO_VAL_INT;
  240. default:
  241. return -EINVAL;
  242. }
  243. }
  244. static int sc27xx_adc_write_raw(struct iio_dev *indio_dev,
  245. struct iio_chan_spec const *chan,
  246. int val, int val2, long mask)
  247. {
  248. struct sc27xx_adc_data *data = iio_priv(indio_dev);
  249. switch (mask) {
  250. case IIO_CHAN_INFO_SCALE:
  251. data->channel_scale[chan->channel] = val;
  252. return IIO_VAL_INT;
  253. default:
  254. return -EINVAL;
  255. }
  256. }
  257. static const struct iio_info sc27xx_info = {
  258. .read_raw = &sc27xx_adc_read_raw,
  259. .write_raw = &sc27xx_adc_write_raw,
  260. };
  261. #define SC27XX_ADC_CHANNEL(index) { \
  262. .type = IIO_VOLTAGE, \
  263. .channel = index, \
  264. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
  265. BIT(IIO_CHAN_INFO_SCALE), \
  266. .datasheet_name = "CH##index", \
  267. .indexed = 1, \
  268. }
  269. static const struct iio_chan_spec sc27xx_channels[] = {
  270. SC27XX_ADC_CHANNEL(0),
  271. SC27XX_ADC_CHANNEL(1),
  272. SC27XX_ADC_CHANNEL(2),
  273. SC27XX_ADC_CHANNEL(3),
  274. SC27XX_ADC_CHANNEL(4),
  275. SC27XX_ADC_CHANNEL(5),
  276. SC27XX_ADC_CHANNEL(6),
  277. SC27XX_ADC_CHANNEL(7),
  278. SC27XX_ADC_CHANNEL(8),
  279. SC27XX_ADC_CHANNEL(9),
  280. SC27XX_ADC_CHANNEL(10),
  281. SC27XX_ADC_CHANNEL(11),
  282. SC27XX_ADC_CHANNEL(12),
  283. SC27XX_ADC_CHANNEL(13),
  284. SC27XX_ADC_CHANNEL(14),
  285. SC27XX_ADC_CHANNEL(15),
  286. SC27XX_ADC_CHANNEL(16),
  287. SC27XX_ADC_CHANNEL(17),
  288. SC27XX_ADC_CHANNEL(18),
  289. SC27XX_ADC_CHANNEL(19),
  290. SC27XX_ADC_CHANNEL(20),
  291. SC27XX_ADC_CHANNEL(21),
  292. SC27XX_ADC_CHANNEL(22),
  293. SC27XX_ADC_CHANNEL(23),
  294. SC27XX_ADC_CHANNEL(24),
  295. SC27XX_ADC_CHANNEL(25),
  296. SC27XX_ADC_CHANNEL(26),
  297. SC27XX_ADC_CHANNEL(27),
  298. SC27XX_ADC_CHANNEL(28),
  299. SC27XX_ADC_CHANNEL(29),
  300. SC27XX_ADC_CHANNEL(30),
  301. SC27XX_ADC_CHANNEL(31),
  302. };
  303. static int sc27xx_adc_enable(struct sc27xx_adc_data *data)
  304. {
  305. int ret;
  306. ret = regmap_update_bits(data->regmap, SC27XX_MODULE_EN,
  307. SC27XX_MODULE_ADC_EN, SC27XX_MODULE_ADC_EN);
  308. if (ret)
  309. return ret;
  310. /* Enable ADC work clock and controller clock */
  311. ret = regmap_update_bits(data->regmap, SC27XX_ARM_CLK_EN,
  312. SC27XX_CLK_ADC_EN | SC27XX_CLK_ADC_CLK_EN,
  313. SC27XX_CLK_ADC_EN | SC27XX_CLK_ADC_CLK_EN);
  314. if (ret)
  315. goto disable_adc;
  316. ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_INT_EN,
  317. SC27XX_ADC_IRQ_EN, SC27XX_ADC_IRQ_EN);
  318. if (ret)
  319. goto disable_clk;
  320. return 0;
  321. disable_clk:
  322. regmap_update_bits(data->regmap, SC27XX_ARM_CLK_EN,
  323. SC27XX_CLK_ADC_EN | SC27XX_CLK_ADC_CLK_EN, 0);
  324. disable_adc:
  325. regmap_update_bits(data->regmap, SC27XX_MODULE_EN,
  326. SC27XX_MODULE_ADC_EN, 0);
  327. return ret;
  328. }
  329. static void sc27xx_adc_disable(void *_data)
  330. {
  331. struct sc27xx_adc_data *data = _data;
  332. regmap_update_bits(data->regmap, data->base + SC27XX_ADC_INT_EN,
  333. SC27XX_ADC_IRQ_EN, 0);
  334. /* Disable ADC work clock and controller clock */
  335. regmap_update_bits(data->regmap, SC27XX_ARM_CLK_EN,
  336. SC27XX_CLK_ADC_EN | SC27XX_CLK_ADC_CLK_EN, 0);
  337. regmap_update_bits(data->regmap, SC27XX_MODULE_EN,
  338. SC27XX_MODULE_ADC_EN, 0);
  339. }
  340. static void sc27xx_adc_free_hwlock(void *_data)
  341. {
  342. struct hwspinlock *hwlock = _data;
  343. hwspin_lock_free(hwlock);
  344. }
  345. static int sc27xx_adc_probe(struct platform_device *pdev)
  346. {
  347. struct device_node *np = pdev->dev.of_node;
  348. struct sc27xx_adc_data *sc27xx_data;
  349. struct iio_dev *indio_dev;
  350. int ret;
  351. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*sc27xx_data));
  352. if (!indio_dev)
  353. return -ENOMEM;
  354. sc27xx_data = iio_priv(indio_dev);
  355. sc27xx_data->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  356. if (!sc27xx_data->regmap) {
  357. dev_err(&pdev->dev, "failed to get ADC regmap\n");
  358. return -ENODEV;
  359. }
  360. ret = of_property_read_u32(np, "reg", &sc27xx_data->base);
  361. if (ret) {
  362. dev_err(&pdev->dev, "failed to get ADC base address\n");
  363. return ret;
  364. }
  365. sc27xx_data->irq = platform_get_irq(pdev, 0);
  366. if (sc27xx_data->irq < 0) {
  367. dev_err(&pdev->dev, "failed to get ADC irq number\n");
  368. return sc27xx_data->irq;
  369. }
  370. ret = of_hwspin_lock_get_id(np, 0);
  371. if (ret < 0) {
  372. dev_err(&pdev->dev, "failed to get hwspinlock id\n");
  373. return ret;
  374. }
  375. sc27xx_data->hwlock = hwspin_lock_request_specific(ret);
  376. if (!sc27xx_data->hwlock) {
  377. dev_err(&pdev->dev, "failed to request hwspinlock\n");
  378. return -ENXIO;
  379. }
  380. ret = devm_add_action(&pdev->dev, sc27xx_adc_free_hwlock,
  381. sc27xx_data->hwlock);
  382. if (ret) {
  383. sc27xx_adc_free_hwlock(sc27xx_data->hwlock);
  384. dev_err(&pdev->dev, "failed to add hwspinlock action\n");
  385. return ret;
  386. }
  387. init_completion(&sc27xx_data->completion);
  388. sc27xx_data->dev = &pdev->dev;
  389. ret = sc27xx_adc_enable(sc27xx_data);
  390. if (ret) {
  391. dev_err(&pdev->dev, "failed to enable ADC module\n");
  392. return ret;
  393. }
  394. ret = devm_add_action(&pdev->dev, sc27xx_adc_disable, sc27xx_data);
  395. if (ret) {
  396. sc27xx_adc_disable(sc27xx_data);
  397. dev_err(&pdev->dev, "failed to add ADC disable action\n");
  398. return ret;
  399. }
  400. ret = devm_request_threaded_irq(&pdev->dev, sc27xx_data->irq, NULL,
  401. sc27xx_adc_isr, IRQF_ONESHOT,
  402. pdev->name, sc27xx_data);
  403. if (ret) {
  404. dev_err(&pdev->dev, "failed to request ADC irq\n");
  405. return ret;
  406. }
  407. indio_dev->dev.parent = &pdev->dev;
  408. indio_dev->name = dev_name(&pdev->dev);
  409. indio_dev->modes = INDIO_DIRECT_MODE;
  410. indio_dev->info = &sc27xx_info;
  411. indio_dev->channels = sc27xx_channels;
  412. indio_dev->num_channels = ARRAY_SIZE(sc27xx_channels);
  413. ret = devm_iio_device_register(&pdev->dev, indio_dev);
  414. if (ret)
  415. dev_err(&pdev->dev, "could not register iio (ADC)");
  416. return ret;
  417. }
  418. static const struct of_device_id sc27xx_adc_of_match[] = {
  419. { .compatible = "sprd,sc2731-adc", },
  420. { }
  421. };
  422. static struct platform_driver sc27xx_adc_driver = {
  423. .probe = sc27xx_adc_probe,
  424. .driver = {
  425. .name = "sc27xx-adc",
  426. .of_match_table = sc27xx_adc_of_match,
  427. },
  428. };
  429. module_platform_driver(sc27xx_adc_driver);
  430. MODULE_AUTHOR("Freeman Liu <freeman.liu@spreadtrum.com>");
  431. MODULE_DESCRIPTION("Spreadtrum SC27XX ADC Driver");
  432. MODULE_LICENSE("GPL v2");