da9150-gpadc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * DA9150 GPADC Driver
  3. *
  4. * Copyright (c) 2014 Dialog Semiconductor
  5. *
  6. * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/mutex.h>
  19. #include <linux/completion.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/machine.h>
  22. #include <linux/iio/driver.h>
  23. #include <linux/mfd/da9150/core.h>
  24. #include <linux/mfd/da9150/registers.h>
  25. /* Channels */
  26. enum da9150_gpadc_hw_channel {
  27. DA9150_GPADC_HW_CHAN_GPIOA_2V = 0,
  28. DA9150_GPADC_HW_CHAN_GPIOA_2V_,
  29. DA9150_GPADC_HW_CHAN_GPIOB_2V,
  30. DA9150_GPADC_HW_CHAN_GPIOB_2V_,
  31. DA9150_GPADC_HW_CHAN_GPIOC_2V,
  32. DA9150_GPADC_HW_CHAN_GPIOC_2V_,
  33. DA9150_GPADC_HW_CHAN_GPIOD_2V,
  34. DA9150_GPADC_HW_CHAN_GPIOD_2V_,
  35. DA9150_GPADC_HW_CHAN_IBUS_SENSE,
  36. DA9150_GPADC_HW_CHAN_IBUS_SENSE_,
  37. DA9150_GPADC_HW_CHAN_VBUS_DIV,
  38. DA9150_GPADC_HW_CHAN_VBUS_DIV_,
  39. DA9150_GPADC_HW_CHAN_ID,
  40. DA9150_GPADC_HW_CHAN_ID_,
  41. DA9150_GPADC_HW_CHAN_VSYS,
  42. DA9150_GPADC_HW_CHAN_VSYS_,
  43. DA9150_GPADC_HW_CHAN_GPIOA_6V,
  44. DA9150_GPADC_HW_CHAN_GPIOA_6V_,
  45. DA9150_GPADC_HW_CHAN_GPIOB_6V,
  46. DA9150_GPADC_HW_CHAN_GPIOB_6V_,
  47. DA9150_GPADC_HW_CHAN_GPIOC_6V,
  48. DA9150_GPADC_HW_CHAN_GPIOC_6V_,
  49. DA9150_GPADC_HW_CHAN_GPIOD_6V,
  50. DA9150_GPADC_HW_CHAN_GPIOD_6V_,
  51. DA9150_GPADC_HW_CHAN_VBAT,
  52. DA9150_GPADC_HW_CHAN_VBAT_,
  53. DA9150_GPADC_HW_CHAN_TBAT,
  54. DA9150_GPADC_HW_CHAN_TBAT_,
  55. DA9150_GPADC_HW_CHAN_TJUNC_CORE,
  56. DA9150_GPADC_HW_CHAN_TJUNC_CORE_,
  57. DA9150_GPADC_HW_CHAN_TJUNC_OVP,
  58. DA9150_GPADC_HW_CHAN_TJUNC_OVP_,
  59. };
  60. enum da9150_gpadc_channel {
  61. DA9150_GPADC_CHAN_GPIOA = 0,
  62. DA9150_GPADC_CHAN_GPIOB,
  63. DA9150_GPADC_CHAN_GPIOC,
  64. DA9150_GPADC_CHAN_GPIOD,
  65. DA9150_GPADC_CHAN_IBUS,
  66. DA9150_GPADC_CHAN_VBUS,
  67. DA9150_GPADC_CHAN_VSYS,
  68. DA9150_GPADC_CHAN_VBAT,
  69. DA9150_GPADC_CHAN_TBAT,
  70. DA9150_GPADC_CHAN_TJUNC_CORE,
  71. DA9150_GPADC_CHAN_TJUNC_OVP,
  72. };
  73. /* Private data */
  74. struct da9150_gpadc {
  75. struct da9150 *da9150;
  76. struct device *dev;
  77. struct mutex lock;
  78. struct completion complete;
  79. };
  80. static irqreturn_t da9150_gpadc_irq(int irq, void *data)
  81. {
  82. struct da9150_gpadc *gpadc = data;
  83. complete(&gpadc->complete);
  84. return IRQ_HANDLED;
  85. }
  86. static int da9150_gpadc_read_adc(struct da9150_gpadc *gpadc, int hw_chan)
  87. {
  88. u8 result_regs[2];
  89. int result;
  90. mutex_lock(&gpadc->lock);
  91. /* Set channel & enable measurement */
  92. da9150_reg_write(gpadc->da9150, DA9150_GPADC_MAN,
  93. (DA9150_GPADC_EN_MASK |
  94. hw_chan << DA9150_GPADC_MUX_SHIFT));
  95. /* Consume left-over completion from a previous timeout */
  96. try_wait_for_completion(&gpadc->complete);
  97. /* Check for actual completion */
  98. wait_for_completion_timeout(&gpadc->complete, msecs_to_jiffies(5));
  99. /* Read result and status from device */
  100. da9150_bulk_read(gpadc->da9150, DA9150_GPADC_RES_A, 2, result_regs);
  101. mutex_unlock(&gpadc->lock);
  102. /* Check to make sure device really has completed reading */
  103. if (result_regs[1] & DA9150_GPADC_RUN_MASK) {
  104. dev_err(gpadc->dev, "Timeout on channel %d of GPADC\n",
  105. hw_chan);
  106. return -ETIMEDOUT;
  107. }
  108. /* LSBs - 2 bits */
  109. result = (result_regs[1] & DA9150_GPADC_RES_L_MASK) >>
  110. DA9150_GPADC_RES_L_SHIFT;
  111. /* MSBs - 8 bits */
  112. result |= result_regs[0] << DA9150_GPADC_RES_L_BITS;
  113. return result;
  114. }
  115. static inline int da9150_gpadc_gpio_6v_voltage_now(int raw_val)
  116. {
  117. /* Convert to mV */
  118. return (6 * ((raw_val * 1000) + 500)) / 1024;
  119. }
  120. static inline int da9150_gpadc_ibus_current_avg(int raw_val)
  121. {
  122. /* Convert to mA */
  123. return (4 * ((raw_val * 1000) + 500)) / 2048;
  124. }
  125. static inline int da9150_gpadc_vbus_21v_voltage_now(int raw_val)
  126. {
  127. /* Convert to mV */
  128. return (21 * ((raw_val * 1000) + 500)) / 1024;
  129. }
  130. static inline int da9150_gpadc_vsys_6v_voltage_now(int raw_val)
  131. {
  132. /* Convert to mV */
  133. return (3 * ((raw_val * 1000) + 500)) / 512;
  134. }
  135. static int da9150_gpadc_read_processed(struct da9150_gpadc *gpadc, int channel,
  136. int hw_chan, int *val)
  137. {
  138. int raw_val;
  139. raw_val = da9150_gpadc_read_adc(gpadc, hw_chan);
  140. if (raw_val < 0)
  141. return raw_val;
  142. switch (channel) {
  143. case DA9150_GPADC_CHAN_GPIOA:
  144. case DA9150_GPADC_CHAN_GPIOB:
  145. case DA9150_GPADC_CHAN_GPIOC:
  146. case DA9150_GPADC_CHAN_GPIOD:
  147. *val = da9150_gpadc_gpio_6v_voltage_now(raw_val);
  148. break;
  149. case DA9150_GPADC_CHAN_IBUS:
  150. *val = da9150_gpadc_ibus_current_avg(raw_val);
  151. break;
  152. case DA9150_GPADC_CHAN_VBUS:
  153. *val = da9150_gpadc_vbus_21v_voltage_now(raw_val);
  154. break;
  155. case DA9150_GPADC_CHAN_VSYS:
  156. *val = da9150_gpadc_vsys_6v_voltage_now(raw_val);
  157. break;
  158. default:
  159. /* No processing for other channels so return raw value */
  160. *val = raw_val;
  161. break;
  162. }
  163. return IIO_VAL_INT;
  164. }
  165. static int da9150_gpadc_read_scale(int channel, int *val, int *val2)
  166. {
  167. switch (channel) {
  168. case DA9150_GPADC_CHAN_VBAT:
  169. *val = 2932;
  170. *val2 = 1000;
  171. return IIO_VAL_FRACTIONAL;
  172. case DA9150_GPADC_CHAN_TJUNC_CORE:
  173. case DA9150_GPADC_CHAN_TJUNC_OVP:
  174. *val = 1000000;
  175. *val2 = 4420;
  176. return IIO_VAL_FRACTIONAL;
  177. default:
  178. return -EINVAL;
  179. }
  180. }
  181. static int da9150_gpadc_read_offset(int channel, int *val)
  182. {
  183. switch (channel) {
  184. case DA9150_GPADC_CHAN_VBAT:
  185. *val = 1500000 / 2932;
  186. return IIO_VAL_INT;
  187. case DA9150_GPADC_CHAN_TJUNC_CORE:
  188. case DA9150_GPADC_CHAN_TJUNC_OVP:
  189. *val = -144;
  190. return IIO_VAL_INT;
  191. default:
  192. return -EINVAL;
  193. }
  194. }
  195. static int da9150_gpadc_read_raw(struct iio_dev *indio_dev,
  196. struct iio_chan_spec const *chan,
  197. int *val, int *val2, long mask)
  198. {
  199. struct da9150_gpadc *gpadc = iio_priv(indio_dev);
  200. if ((chan->channel < DA9150_GPADC_CHAN_GPIOA) ||
  201. (chan->channel > DA9150_GPADC_CHAN_TJUNC_OVP))
  202. return -EINVAL;
  203. switch (mask) {
  204. case IIO_CHAN_INFO_RAW:
  205. case IIO_CHAN_INFO_PROCESSED:
  206. return da9150_gpadc_read_processed(gpadc, chan->channel,
  207. chan->address, val);
  208. case IIO_CHAN_INFO_SCALE:
  209. return da9150_gpadc_read_scale(chan->channel, val, val2);
  210. case IIO_CHAN_INFO_OFFSET:
  211. return da9150_gpadc_read_offset(chan->channel, val);
  212. default:
  213. return -EINVAL;
  214. }
  215. }
  216. static const struct iio_info da9150_gpadc_info = {
  217. .read_raw = &da9150_gpadc_read_raw,
  218. .driver_module = THIS_MODULE,
  219. };
  220. #define DA9150_GPADC_CHANNEL(_id, _hw_id, _type, chan_info, \
  221. _ext_name) { \
  222. .type = _type, \
  223. .indexed = 1, \
  224. .channel = DA9150_GPADC_CHAN_##_id, \
  225. .address = DA9150_GPADC_HW_CHAN_##_hw_id, \
  226. .info_mask_separate = chan_info, \
  227. .extend_name = _ext_name, \
  228. .datasheet_name = #_id, \
  229. }
  230. #define DA9150_GPADC_CHANNEL_RAW(_id, _hw_id, _type, _ext_name) \
  231. DA9150_GPADC_CHANNEL(_id, _hw_id, _type, \
  232. BIT(IIO_CHAN_INFO_RAW), _ext_name)
  233. #define DA9150_GPADC_CHANNEL_SCALED(_id, _hw_id, _type, _ext_name) \
  234. DA9150_GPADC_CHANNEL(_id, _hw_id, _type, \
  235. BIT(IIO_CHAN_INFO_RAW) | \
  236. BIT(IIO_CHAN_INFO_SCALE) | \
  237. BIT(IIO_CHAN_INFO_OFFSET), \
  238. _ext_name)
  239. #define DA9150_GPADC_CHANNEL_PROCESSED(_id, _hw_id, _type, _ext_name) \
  240. DA9150_GPADC_CHANNEL(_id, _hw_id, _type, \
  241. BIT(IIO_CHAN_INFO_PROCESSED), _ext_name)
  242. /* Supported channels */
  243. static const struct iio_chan_spec da9150_gpadc_channels[] = {
  244. DA9150_GPADC_CHANNEL_PROCESSED(GPIOA, GPIOA_6V, IIO_VOLTAGE, NULL),
  245. DA9150_GPADC_CHANNEL_PROCESSED(GPIOB, GPIOB_6V, IIO_VOLTAGE, NULL),
  246. DA9150_GPADC_CHANNEL_PROCESSED(GPIOC, GPIOC_6V, IIO_VOLTAGE, NULL),
  247. DA9150_GPADC_CHANNEL_PROCESSED(GPIOD, GPIOD_6V, IIO_VOLTAGE, NULL),
  248. DA9150_GPADC_CHANNEL_PROCESSED(IBUS, IBUS_SENSE, IIO_CURRENT, "ibus"),
  249. DA9150_GPADC_CHANNEL_PROCESSED(VBUS, VBUS_DIV_, IIO_VOLTAGE, "vbus"),
  250. DA9150_GPADC_CHANNEL_PROCESSED(VSYS, VSYS, IIO_VOLTAGE, "vsys"),
  251. DA9150_GPADC_CHANNEL_SCALED(VBAT, VBAT, IIO_VOLTAGE, "vbat"),
  252. DA9150_GPADC_CHANNEL_RAW(TBAT, TBAT, IIO_VOLTAGE, "tbat"),
  253. DA9150_GPADC_CHANNEL_SCALED(TJUNC_CORE, TJUNC_CORE, IIO_TEMP,
  254. "tjunc_core"),
  255. DA9150_GPADC_CHANNEL_SCALED(TJUNC_OVP, TJUNC_OVP, IIO_TEMP,
  256. "tjunc_ovp"),
  257. };
  258. /* Default maps used by da9150-charger */
  259. static struct iio_map da9150_gpadc_default_maps[] = {
  260. {
  261. .consumer_dev_name = "da9150-charger",
  262. .consumer_channel = "CHAN_IBUS",
  263. .adc_channel_label = "IBUS",
  264. },
  265. {
  266. .consumer_dev_name = "da9150-charger",
  267. .consumer_channel = "CHAN_VBUS",
  268. .adc_channel_label = "VBUS",
  269. },
  270. {
  271. .consumer_dev_name = "da9150-charger",
  272. .consumer_channel = "CHAN_TJUNC",
  273. .adc_channel_label = "TJUNC_CORE",
  274. },
  275. {
  276. .consumer_dev_name = "da9150-charger",
  277. .consumer_channel = "CHAN_VBAT",
  278. .adc_channel_label = "VBAT",
  279. },
  280. {},
  281. };
  282. static int da9150_gpadc_probe(struct platform_device *pdev)
  283. {
  284. struct device *dev = &pdev->dev;
  285. struct da9150 *da9150 = dev_get_drvdata(dev->parent);
  286. struct da9150_gpadc *gpadc;
  287. struct iio_dev *indio_dev;
  288. int irq, ret;
  289. indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
  290. if (!indio_dev) {
  291. dev_err(&pdev->dev, "Failed to allocate IIO device\n");
  292. return -ENOMEM;
  293. }
  294. gpadc = iio_priv(indio_dev);
  295. platform_set_drvdata(pdev, indio_dev);
  296. gpadc->da9150 = da9150;
  297. gpadc->dev = dev;
  298. mutex_init(&gpadc->lock);
  299. init_completion(&gpadc->complete);
  300. irq = platform_get_irq_byname(pdev, "GPADC");
  301. if (irq < 0) {
  302. dev_err(dev, "Failed to get IRQ: %d\n", irq);
  303. return irq;
  304. }
  305. ret = devm_request_threaded_irq(dev, irq, NULL, da9150_gpadc_irq,
  306. IRQF_ONESHOT, "GPADC", gpadc);
  307. if (ret) {
  308. dev_err(dev, "Failed to request IRQ %d: %d\n", irq, ret);
  309. return ret;
  310. }
  311. ret = iio_map_array_register(indio_dev, da9150_gpadc_default_maps);
  312. if (ret) {
  313. dev_err(dev, "Failed to register IIO maps: %d\n", ret);
  314. return ret;
  315. }
  316. indio_dev->name = dev_name(dev);
  317. indio_dev->dev.parent = dev;
  318. indio_dev->dev.of_node = pdev->dev.of_node;
  319. indio_dev->info = &da9150_gpadc_info;
  320. indio_dev->modes = INDIO_DIRECT_MODE;
  321. indio_dev->channels = da9150_gpadc_channels;
  322. indio_dev->num_channels = ARRAY_SIZE(da9150_gpadc_channels);
  323. ret = iio_device_register(indio_dev);
  324. if (ret) {
  325. dev_err(dev, "Failed to register IIO device: %d\n", ret);
  326. goto iio_map_unreg;
  327. }
  328. return 0;
  329. iio_map_unreg:
  330. iio_map_array_unregister(indio_dev);
  331. return ret;
  332. }
  333. static int da9150_gpadc_remove(struct platform_device *pdev)
  334. {
  335. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  336. iio_device_unregister(indio_dev);
  337. iio_map_array_unregister(indio_dev);
  338. return 0;
  339. }
  340. static struct platform_driver da9150_gpadc_driver = {
  341. .driver = {
  342. .name = "da9150-gpadc",
  343. },
  344. .probe = da9150_gpadc_probe,
  345. .remove = da9150_gpadc_remove,
  346. };
  347. module_platform_driver(da9150_gpadc_driver);
  348. MODULE_DESCRIPTION("GPADC Driver for DA9150");
  349. MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
  350. MODULE_LICENSE("GPL");