cpcap-adc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /*
  2. * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
  3. *
  4. * Rewritten for Linux IIO framework with some code based on
  5. * earlier driver found in the Motorola Linux kernel:
  6. *
  7. * Copyright (C) 2009-2010 Motorola, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/delay.h>
  19. #include <linux/device.h>
  20. #include <linux/err.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/regmap.h>
  29. #include <linux/iio/buffer.h>
  30. #include <linux/iio/driver.h>
  31. #include <linux/iio/iio.h>
  32. #include <linux/iio/kfifo_buf.h>
  33. #include <linux/mfd/motorola-cpcap.h>
  34. /* Register CPCAP_REG_ADCC1 bits */
  35. #define CPCAP_BIT_ADEN_AUTO_CLR BIT(15) /* Currently unused */
  36. #define CPCAP_BIT_CAL_MODE BIT(14) /* Set with BIT_RAND0 */
  37. #define CPCAP_BIT_ADC_CLK_SEL1 BIT(13) /* Currently unused */
  38. #define CPCAP_BIT_ADC_CLK_SEL0 BIT(12) /* Currently unused */
  39. #define CPCAP_BIT_ATOX BIT(11)
  40. #define CPCAP_BIT_ATO3 BIT(10)
  41. #define CPCAP_BIT_ATO2 BIT(9)
  42. #define CPCAP_BIT_ATO1 BIT(8)
  43. #define CPCAP_BIT_ATO0 BIT(7)
  44. #define CPCAP_BIT_ADA2 BIT(6)
  45. #define CPCAP_BIT_ADA1 BIT(5)
  46. #define CPCAP_BIT_ADA0 BIT(4)
  47. #define CPCAP_BIT_AD_SEL1 BIT(3) /* Set for bank1 */
  48. #define CPCAP_BIT_RAND1 BIT(2) /* Set for channel 16 & 17 */
  49. #define CPCAP_BIT_RAND0 BIT(1) /* Set with CAL_MODE */
  50. #define CPCAP_BIT_ADEN BIT(0) /* Currently unused */
  51. #define CPCAP_REG_ADCC1_DEFAULTS (CPCAP_BIT_ADEN_AUTO_CLR | \
  52. CPCAP_BIT_ADC_CLK_SEL0 | \
  53. CPCAP_BIT_RAND1)
  54. /* Register CPCAP_REG_ADCC2 bits */
  55. #define CPCAP_BIT_CAL_FACTOR_ENABLE BIT(15) /* Currently unused */
  56. #define CPCAP_BIT_BATDETB_EN BIT(14) /* Currently unused */
  57. #define CPCAP_BIT_ADTRIG_ONESHOT BIT(13) /* Set for !TIMING_IMM */
  58. #define CPCAP_BIT_ASC BIT(12) /* Set for TIMING_IMM */
  59. #define CPCAP_BIT_ATOX_PS_FACTOR BIT(11)
  60. #define CPCAP_BIT_ADC_PS_FACTOR1 BIT(10)
  61. #define CPCAP_BIT_ADC_PS_FACTOR0 BIT(9)
  62. #define CPCAP_BIT_AD4_SELECT BIT(8) /* Currently unused */
  63. #define CPCAP_BIT_ADC_BUSY BIT(7) /* Currently unused */
  64. #define CPCAP_BIT_THERMBIAS_EN BIT(6) /* Bias for AD0_BATTDETB */
  65. #define CPCAP_BIT_ADTRIG_DIS BIT(5) /* Disable interrupt */
  66. #define CPCAP_BIT_LIADC BIT(4) /* Currently unused */
  67. #define CPCAP_BIT_TS_REFEN BIT(3) /* Currently unused */
  68. #define CPCAP_BIT_TS_M2 BIT(2) /* Currently unused */
  69. #define CPCAP_BIT_TS_M1 BIT(1) /* Currently unused */
  70. #define CPCAP_BIT_TS_M0 BIT(0) /* Currently unused */
  71. #define CPCAP_REG_ADCC2_DEFAULTS (CPCAP_BIT_AD4_SELECT | \
  72. CPCAP_BIT_ADTRIG_DIS | \
  73. CPCAP_BIT_LIADC | \
  74. CPCAP_BIT_TS_M2 | \
  75. CPCAP_BIT_TS_M1)
  76. #define CPCAP_MAX_TEMP_LVL 27
  77. #define CPCAP_FOUR_POINT_TWO_ADC 801
  78. #define ST_ADC_CAL_CHRGI_HIGH_THRESHOLD 530
  79. #define ST_ADC_CAL_CHRGI_LOW_THRESHOLD 494
  80. #define ST_ADC_CAL_BATTI_HIGH_THRESHOLD 530
  81. #define ST_ADC_CAL_BATTI_LOW_THRESHOLD 494
  82. #define ST_ADC_CALIBRATE_DIFF_THRESHOLD 3
  83. #define CPCAP_ADC_MAX_RETRIES 5 /* Calibration */
  84. /**
  85. * struct cpcap_adc_ato - timing settings for cpcap adc
  86. *
  87. * Unfortunately no cpcap documentation available, please document when
  88. * using these.
  89. */
  90. struct cpcap_adc_ato {
  91. unsigned short ato_in;
  92. unsigned short atox_in;
  93. unsigned short adc_ps_factor_in;
  94. unsigned short atox_ps_factor_in;
  95. unsigned short ato_out;
  96. unsigned short atox_out;
  97. unsigned short adc_ps_factor_out;
  98. unsigned short atox_ps_factor_out;
  99. };
  100. /**
  101. * struct cpcap-adc - cpcap adc device driver data
  102. * @reg: cpcap regmap
  103. * @dev: struct device
  104. * @vendor: cpcap vendor
  105. * @irq: interrupt
  106. * @lock: mutex
  107. * @ato: request timings
  108. * @wq_data_avail: work queue
  109. * @done: work done
  110. */
  111. struct cpcap_adc {
  112. struct regmap *reg;
  113. struct device *dev;
  114. u16 vendor;
  115. int irq;
  116. struct mutex lock; /* ADC register access lock */
  117. const struct cpcap_adc_ato *ato;
  118. wait_queue_head_t wq_data_avail;
  119. bool done;
  120. };
  121. /**
  122. * enum cpcap_adc_channel - cpcap adc channels
  123. */
  124. enum cpcap_adc_channel {
  125. /* Bank0 channels */
  126. CPCAP_ADC_AD0, /* Battery temperature */
  127. CPCAP_ADC_BATTP, /* Battery voltage */
  128. CPCAP_ADC_VBUS, /* USB VBUS voltage */
  129. CPCAP_ADC_AD3, /* Die temperature when charging */
  130. CPCAP_ADC_BPLUS_AD4, /* Another battery or system voltage */
  131. CPCAP_ADC_CHG_ISENSE, /* Calibrated charge current */
  132. CPCAP_ADC_BATTI, /* Calibrated system current */
  133. CPCAP_ADC_USB_ID, /* USB OTG ID, unused on droid 4? */
  134. /* Bank1 channels */
  135. CPCAP_ADC_AD8, /* Seems unused */
  136. CPCAP_ADC_AD9, /* Seems unused */
  137. CPCAP_ADC_LICELL, /* Maybe system voltage? Always 3V */
  138. CPCAP_ADC_HV_BATTP, /* Another battery detection? */
  139. CPCAP_ADC_TSX1_AD12, /* Seems unused, for touchscreen? */
  140. CPCAP_ADC_TSX2_AD13, /* Seems unused, for touchscreen? */
  141. CPCAP_ADC_TSY1_AD14, /* Seems unused, for touchscreen? */
  142. CPCAP_ADC_TSY2_AD15, /* Seems unused, for touchscreen? */
  143. /* Remuxed channels using bank0 entries */
  144. CPCAP_ADC_BATTP_PI16, /* Alternative mux mode for BATTP */
  145. CPCAP_ADC_BATTI_PI17, /* Alternative mux mode for BATTI */
  146. CPCAP_ADC_CHANNEL_NUM,
  147. };
  148. /**
  149. * enum cpcap_adc_timing - cpcap adc timing options
  150. *
  151. * CPCAP_ADC_TIMING_IMM seems to be immediate with no timings.
  152. * Please document when using.
  153. */
  154. enum cpcap_adc_timing {
  155. CPCAP_ADC_TIMING_IMM,
  156. CPCAP_ADC_TIMING_IN,
  157. CPCAP_ADC_TIMING_OUT,
  158. };
  159. /**
  160. * struct cpcap_adc_phasing_tbl - cpcap phasing table
  161. * @offset: offset in the phasing table
  162. * @multiplier: multiplier in the phasing table
  163. * @divider: divider in the phasing table
  164. * @min: minimum value
  165. * @max: maximum value
  166. */
  167. struct cpcap_adc_phasing_tbl {
  168. short offset;
  169. unsigned short multiplier;
  170. unsigned short divider;
  171. short min;
  172. short max;
  173. };
  174. /**
  175. * struct cpcap_adc_conversion_tbl - cpcap conversion table
  176. * @conv_type: conversion type
  177. * @align_offset: align offset
  178. * @conv_offset: conversion offset
  179. * @cal_offset: calibration offset
  180. * @multiplier: conversion multiplier
  181. * @divider: conversion divider
  182. */
  183. struct cpcap_adc_conversion_tbl {
  184. enum iio_chan_info_enum conv_type;
  185. int align_offset;
  186. int conv_offset;
  187. int cal_offset;
  188. int multiplier;
  189. int divider;
  190. };
  191. /**
  192. * struct cpcap_adc_request - cpcap adc request
  193. * @channel: request channel
  194. * @phase_tbl: channel phasing table
  195. * @conv_tbl: channel conversion table
  196. * @bank_index: channel index within the bank
  197. * @timing: timing settings
  198. * @result: result
  199. */
  200. struct cpcap_adc_request {
  201. int channel;
  202. const struct cpcap_adc_phasing_tbl *phase_tbl;
  203. const struct cpcap_adc_conversion_tbl *conv_tbl;
  204. int bank_index;
  205. enum cpcap_adc_timing timing;
  206. int result;
  207. };
  208. /* Phasing table for channels. Note that channels 16 & 17 use BATTP and BATTI */
  209. static const struct cpcap_adc_phasing_tbl bank_phasing[] = {
  210. /* Bank0 */
  211. [CPCAP_ADC_AD0] = {0, 0x80, 0x80, 0, 1023},
  212. [CPCAP_ADC_BATTP] = {0, 0x80, 0x80, 0, 1023},
  213. [CPCAP_ADC_VBUS] = {0, 0x80, 0x80, 0, 1023},
  214. [CPCAP_ADC_AD3] = {0, 0x80, 0x80, 0, 1023},
  215. [CPCAP_ADC_BPLUS_AD4] = {0, 0x80, 0x80, 0, 1023},
  216. [CPCAP_ADC_CHG_ISENSE] = {0, 0x80, 0x80, -512, 511},
  217. [CPCAP_ADC_BATTI] = {0, 0x80, 0x80, -512, 511},
  218. [CPCAP_ADC_USB_ID] = {0, 0x80, 0x80, 0, 1023},
  219. /* Bank1 */
  220. [CPCAP_ADC_AD8] = {0, 0x80, 0x80, 0, 1023},
  221. [CPCAP_ADC_AD9] = {0, 0x80, 0x80, 0, 1023},
  222. [CPCAP_ADC_LICELL] = {0, 0x80, 0x80, 0, 1023},
  223. [CPCAP_ADC_HV_BATTP] = {0, 0x80, 0x80, 0, 1023},
  224. [CPCAP_ADC_TSX1_AD12] = {0, 0x80, 0x80, 0, 1023},
  225. [CPCAP_ADC_TSX2_AD13] = {0, 0x80, 0x80, 0, 1023},
  226. [CPCAP_ADC_TSY1_AD14] = {0, 0x80, 0x80, 0, 1023},
  227. [CPCAP_ADC_TSY2_AD15] = {0, 0x80, 0x80, 0, 1023},
  228. };
  229. /*
  230. * Conversion table for channels. Updated during init based on calibration.
  231. * Here too channels 16 & 17 use BATTP and BATTI.
  232. */
  233. static struct cpcap_adc_conversion_tbl bank_conversion[] = {
  234. /* Bank0 */
  235. [CPCAP_ADC_AD0] = {
  236. IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 1, 1,
  237. },
  238. [CPCAP_ADC_BATTP] = {
  239. IIO_CHAN_INFO_PROCESSED, 0, 2400, 0, 2300, 1023,
  240. },
  241. [CPCAP_ADC_VBUS] = {
  242. IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 10000, 1023,
  243. },
  244. [CPCAP_ADC_AD3] = {
  245. IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 1, 1,
  246. },
  247. [CPCAP_ADC_BPLUS_AD4] = {
  248. IIO_CHAN_INFO_PROCESSED, 0, 2400, 0, 2300, 1023,
  249. },
  250. [CPCAP_ADC_CHG_ISENSE] = {
  251. IIO_CHAN_INFO_PROCESSED, -512, 2, 0, 5000, 1023,
  252. },
  253. [CPCAP_ADC_BATTI] = {
  254. IIO_CHAN_INFO_PROCESSED, -512, 2, 0, 5000, 1023,
  255. },
  256. [CPCAP_ADC_USB_ID] = {
  257. IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
  258. },
  259. /* Bank1 */
  260. [CPCAP_ADC_AD8] = {
  261. IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
  262. },
  263. [CPCAP_ADC_AD9] = {
  264. IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
  265. },
  266. [CPCAP_ADC_LICELL] = {
  267. IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 3400, 1023,
  268. },
  269. [CPCAP_ADC_HV_BATTP] = {
  270. IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
  271. },
  272. [CPCAP_ADC_TSX1_AD12] = {
  273. IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
  274. },
  275. [CPCAP_ADC_TSX2_AD13] = {
  276. IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
  277. },
  278. [CPCAP_ADC_TSY1_AD14] = {
  279. IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
  280. },
  281. [CPCAP_ADC_TSY2_AD15] = {
  282. IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
  283. },
  284. };
  285. /*
  286. * Temperature lookup table of register values to milliCelcius.
  287. * REVISIT: Check the duplicate 0x3ff entry in a freezer
  288. */
  289. static const int temp_map[CPCAP_MAX_TEMP_LVL][2] = {
  290. { 0x03ff, -40000 },
  291. { 0x03ff, -35000 },
  292. { 0x03ef, -30000 },
  293. { 0x03b2, -25000 },
  294. { 0x036c, -20000 },
  295. { 0x0320, -15000 },
  296. { 0x02d0, -10000 },
  297. { 0x027f, -5000 },
  298. { 0x022f, 0 },
  299. { 0x01e4, 5000 },
  300. { 0x019f, 10000 },
  301. { 0x0161, 15000 },
  302. { 0x012b, 20000 },
  303. { 0x00fc, 25000 },
  304. { 0x00d4, 30000 },
  305. { 0x00b2, 35000 },
  306. { 0x0095, 40000 },
  307. { 0x007d, 45000 },
  308. { 0x0069, 50000 },
  309. { 0x0059, 55000 },
  310. { 0x004b, 60000 },
  311. { 0x003f, 65000 },
  312. { 0x0036, 70000 },
  313. { 0x002e, 75000 },
  314. { 0x0027, 80000 },
  315. { 0x0022, 85000 },
  316. { 0x001d, 90000 },
  317. };
  318. #define CPCAP_CHAN(_type, _index, _address, _datasheet_name) { \
  319. .type = (_type), \
  320. .address = (_address), \
  321. .indexed = 1, \
  322. .channel = (_index), \
  323. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  324. BIT(IIO_CHAN_INFO_PROCESSED), \
  325. .scan_index = (_index), \
  326. .scan_type = { \
  327. .sign = 'u', \
  328. .realbits = 10, \
  329. .storagebits = 16, \
  330. .endianness = IIO_CPU, \
  331. }, \
  332. .datasheet_name = (_datasheet_name), \
  333. }
  334. /*
  335. * The datasheet names are from Motorola mapphone Linux kernel except
  336. * for the last two which might be uncalibrated charge voltage and
  337. * current.
  338. */
  339. static const struct iio_chan_spec cpcap_adc_channels[] = {
  340. /* Bank0 */
  341. CPCAP_CHAN(IIO_TEMP, 0, CPCAP_REG_ADCD0, "battdetb"),
  342. CPCAP_CHAN(IIO_VOLTAGE, 1, CPCAP_REG_ADCD1, "battp"),
  343. CPCAP_CHAN(IIO_VOLTAGE, 2, CPCAP_REG_ADCD2, "vbus"),
  344. CPCAP_CHAN(IIO_TEMP, 3, CPCAP_REG_ADCD3, "ad3"),
  345. CPCAP_CHAN(IIO_VOLTAGE, 4, CPCAP_REG_ADCD4, "ad4"),
  346. CPCAP_CHAN(IIO_CURRENT, 5, CPCAP_REG_ADCD5, "chg_isense"),
  347. CPCAP_CHAN(IIO_CURRENT, 6, CPCAP_REG_ADCD6, "batti"),
  348. CPCAP_CHAN(IIO_VOLTAGE, 7, CPCAP_REG_ADCD7, "usb_id"),
  349. /* Bank1 */
  350. CPCAP_CHAN(IIO_CURRENT, 8, CPCAP_REG_ADCD0, "ad8"),
  351. CPCAP_CHAN(IIO_VOLTAGE, 9, CPCAP_REG_ADCD1, "ad9"),
  352. CPCAP_CHAN(IIO_VOLTAGE, 10, CPCAP_REG_ADCD2, "licell"),
  353. CPCAP_CHAN(IIO_VOLTAGE, 11, CPCAP_REG_ADCD3, "hv_battp"),
  354. CPCAP_CHAN(IIO_VOLTAGE, 12, CPCAP_REG_ADCD4, "tsx1_ad12"),
  355. CPCAP_CHAN(IIO_VOLTAGE, 13, CPCAP_REG_ADCD5, "tsx2_ad13"),
  356. CPCAP_CHAN(IIO_VOLTAGE, 14, CPCAP_REG_ADCD6, "tsy1_ad14"),
  357. CPCAP_CHAN(IIO_VOLTAGE, 15, CPCAP_REG_ADCD7, "tsy2_ad15"),
  358. /* There are two registers with multiplexed functionality */
  359. CPCAP_CHAN(IIO_VOLTAGE, 16, CPCAP_REG_ADCD0, "chg_vsense"),
  360. CPCAP_CHAN(IIO_CURRENT, 17, CPCAP_REG_ADCD1, "batti2"),
  361. };
  362. static irqreturn_t cpcap_adc_irq_thread(int irq, void *data)
  363. {
  364. struct iio_dev *indio_dev = data;
  365. struct cpcap_adc *ddata = iio_priv(indio_dev);
  366. int error;
  367. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
  368. CPCAP_BIT_ADTRIG_DIS,
  369. CPCAP_BIT_ADTRIG_DIS);
  370. if (error)
  371. return IRQ_NONE;
  372. ddata->done = true;
  373. wake_up_interruptible(&ddata->wq_data_avail);
  374. return IRQ_HANDLED;
  375. }
  376. /* ADC calibration functions */
  377. static void cpcap_adc_setup_calibrate(struct cpcap_adc *ddata,
  378. enum cpcap_adc_channel chan)
  379. {
  380. unsigned int value = 0;
  381. unsigned long timeout = jiffies + msecs_to_jiffies(3000);
  382. int error;
  383. if ((chan != CPCAP_ADC_CHG_ISENSE) &&
  384. (chan != CPCAP_ADC_BATTI))
  385. return;
  386. value |= CPCAP_BIT_CAL_MODE | CPCAP_BIT_RAND0;
  387. value |= ((chan << 4) &
  388. (CPCAP_BIT_ADA2 | CPCAP_BIT_ADA1 | CPCAP_BIT_ADA0));
  389. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1,
  390. CPCAP_BIT_CAL_MODE | CPCAP_BIT_ATOX |
  391. CPCAP_BIT_ATO3 | CPCAP_BIT_ATO2 |
  392. CPCAP_BIT_ATO1 | CPCAP_BIT_ATO0 |
  393. CPCAP_BIT_ADA2 | CPCAP_BIT_ADA1 |
  394. CPCAP_BIT_ADA0 | CPCAP_BIT_AD_SEL1 |
  395. CPCAP_BIT_RAND1 | CPCAP_BIT_RAND0,
  396. value);
  397. if (error)
  398. return;
  399. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
  400. CPCAP_BIT_ATOX_PS_FACTOR |
  401. CPCAP_BIT_ADC_PS_FACTOR1 |
  402. CPCAP_BIT_ADC_PS_FACTOR0,
  403. 0);
  404. if (error)
  405. return;
  406. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
  407. CPCAP_BIT_ADTRIG_DIS,
  408. CPCAP_BIT_ADTRIG_DIS);
  409. if (error)
  410. return;
  411. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
  412. CPCAP_BIT_ASC,
  413. CPCAP_BIT_ASC);
  414. if (error)
  415. return;
  416. do {
  417. schedule_timeout_uninterruptible(1);
  418. error = regmap_read(ddata->reg, CPCAP_REG_ADCC2, &value);
  419. if (error)
  420. return;
  421. } while ((value & CPCAP_BIT_ASC) && time_before(jiffies, timeout));
  422. if (value & CPCAP_BIT_ASC)
  423. dev_err(ddata->dev,
  424. "Timeout waiting for calibration to complete\n");
  425. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1,
  426. CPCAP_BIT_CAL_MODE, 0);
  427. if (error)
  428. return;
  429. }
  430. static int cpcap_adc_calibrate_one(struct cpcap_adc *ddata,
  431. int channel,
  432. u16 calibration_register,
  433. int lower_threshold,
  434. int upper_threshold)
  435. {
  436. unsigned int calibration_data[2];
  437. unsigned short cal_data_diff;
  438. int i, error;
  439. for (i = 0; i < CPCAP_ADC_MAX_RETRIES; i++) {
  440. calibration_data[0] = 0;
  441. calibration_data[1] = 0;
  442. cal_data_diff = 0;
  443. cpcap_adc_setup_calibrate(ddata, channel);
  444. error = regmap_read(ddata->reg, calibration_register,
  445. &calibration_data[0]);
  446. if (error)
  447. return error;
  448. cpcap_adc_setup_calibrate(ddata, channel);
  449. error = regmap_read(ddata->reg, calibration_register,
  450. &calibration_data[1]);
  451. if (error)
  452. return error;
  453. if (calibration_data[0] > calibration_data[1])
  454. cal_data_diff =
  455. calibration_data[0] - calibration_data[1];
  456. else
  457. cal_data_diff =
  458. calibration_data[1] - calibration_data[0];
  459. if (((calibration_data[1] >= lower_threshold) &&
  460. (calibration_data[1] <= upper_threshold) &&
  461. (cal_data_diff <= ST_ADC_CALIBRATE_DIFF_THRESHOLD)) ||
  462. (ddata->vendor == CPCAP_VENDOR_TI)) {
  463. bank_conversion[channel].cal_offset =
  464. ((short)calibration_data[1] * -1) + 512;
  465. dev_dbg(ddata->dev, "ch%i calibration complete: %i\n",
  466. channel, bank_conversion[channel].cal_offset);
  467. break;
  468. }
  469. usleep_range(5000, 10000);
  470. }
  471. return 0;
  472. }
  473. static int cpcap_adc_calibrate(struct cpcap_adc *ddata)
  474. {
  475. int error;
  476. error = cpcap_adc_calibrate_one(ddata, CPCAP_ADC_CHG_ISENSE,
  477. CPCAP_REG_ADCAL1,
  478. ST_ADC_CAL_CHRGI_LOW_THRESHOLD,
  479. ST_ADC_CAL_CHRGI_HIGH_THRESHOLD);
  480. if (error)
  481. return error;
  482. error = cpcap_adc_calibrate_one(ddata, CPCAP_ADC_BATTI,
  483. CPCAP_REG_ADCAL2,
  484. ST_ADC_CAL_BATTI_LOW_THRESHOLD,
  485. ST_ADC_CAL_BATTI_HIGH_THRESHOLD);
  486. if (error)
  487. return error;
  488. return 0;
  489. }
  490. /* ADC setup, read and scale functions */
  491. static void cpcap_adc_setup_bank(struct cpcap_adc *ddata,
  492. struct cpcap_adc_request *req)
  493. {
  494. const struct cpcap_adc_ato *ato = ddata->ato;
  495. unsigned short value1 = 0;
  496. unsigned short value2 = 0;
  497. int error;
  498. if (!ato)
  499. return;
  500. switch (req->channel) {
  501. case CPCAP_ADC_AD0:
  502. value2 |= CPCAP_BIT_THERMBIAS_EN;
  503. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
  504. CPCAP_BIT_THERMBIAS_EN,
  505. value2);
  506. if (error)
  507. return;
  508. usleep_range(800, 1000);
  509. break;
  510. case CPCAP_ADC_AD8 ... CPCAP_ADC_TSY2_AD15:
  511. value1 |= CPCAP_BIT_AD_SEL1;
  512. break;
  513. case CPCAP_ADC_BATTP_PI16 ... CPCAP_ADC_BATTI_PI17:
  514. value1 |= CPCAP_BIT_RAND1;
  515. default:
  516. break;
  517. }
  518. switch (req->timing) {
  519. case CPCAP_ADC_TIMING_IN:
  520. value1 |= ato->ato_in;
  521. value1 |= ato->atox_in;
  522. value2 |= ato->adc_ps_factor_in;
  523. value2 |= ato->atox_ps_factor_in;
  524. break;
  525. case CPCAP_ADC_TIMING_OUT:
  526. value1 |= ato->ato_out;
  527. value1 |= ato->atox_out;
  528. value2 |= ato->adc_ps_factor_out;
  529. value2 |= ato->atox_ps_factor_out;
  530. break;
  531. case CPCAP_ADC_TIMING_IMM:
  532. default:
  533. break;
  534. }
  535. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1,
  536. CPCAP_BIT_CAL_MODE | CPCAP_BIT_ATOX |
  537. CPCAP_BIT_ATO3 | CPCAP_BIT_ATO2 |
  538. CPCAP_BIT_ATO1 | CPCAP_BIT_ATO0 |
  539. CPCAP_BIT_ADA2 | CPCAP_BIT_ADA1 |
  540. CPCAP_BIT_ADA0 | CPCAP_BIT_AD_SEL1 |
  541. CPCAP_BIT_RAND1 | CPCAP_BIT_RAND0,
  542. value1);
  543. if (error)
  544. return;
  545. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
  546. CPCAP_BIT_ATOX_PS_FACTOR |
  547. CPCAP_BIT_ADC_PS_FACTOR1 |
  548. CPCAP_BIT_ADC_PS_FACTOR0 |
  549. CPCAP_BIT_THERMBIAS_EN,
  550. value2);
  551. if (error)
  552. return;
  553. if (req->timing == CPCAP_ADC_TIMING_IMM) {
  554. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
  555. CPCAP_BIT_ADTRIG_DIS,
  556. CPCAP_BIT_ADTRIG_DIS);
  557. if (error)
  558. return;
  559. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
  560. CPCAP_BIT_ASC,
  561. CPCAP_BIT_ASC);
  562. if (error)
  563. return;
  564. } else {
  565. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
  566. CPCAP_BIT_ADTRIG_ONESHOT,
  567. CPCAP_BIT_ADTRIG_ONESHOT);
  568. if (error)
  569. return;
  570. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
  571. CPCAP_BIT_ADTRIG_DIS, 0);
  572. if (error)
  573. return;
  574. }
  575. }
  576. static int cpcap_adc_start_bank(struct cpcap_adc *ddata,
  577. struct cpcap_adc_request *req)
  578. {
  579. int i, error;
  580. req->timing = CPCAP_ADC_TIMING_IMM;
  581. ddata->done = false;
  582. for (i = 0; i < CPCAP_ADC_MAX_RETRIES; i++) {
  583. cpcap_adc_setup_bank(ddata, req);
  584. error = wait_event_interruptible_timeout(ddata->wq_data_avail,
  585. ddata->done,
  586. msecs_to_jiffies(50));
  587. if (error > 0)
  588. return 0;
  589. if (error == 0) {
  590. error = -ETIMEDOUT;
  591. continue;
  592. }
  593. if (error < 0)
  594. return error;
  595. }
  596. return error;
  597. }
  598. static int cpcap_adc_stop_bank(struct cpcap_adc *ddata)
  599. {
  600. int error;
  601. error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1,
  602. 0xffff,
  603. CPCAP_REG_ADCC1_DEFAULTS);
  604. if (error)
  605. return error;
  606. return regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
  607. 0xffff,
  608. CPCAP_REG_ADCC2_DEFAULTS);
  609. }
  610. static void cpcap_adc_phase(struct cpcap_adc_request *req)
  611. {
  612. const struct cpcap_adc_conversion_tbl *conv_tbl = req->conv_tbl;
  613. const struct cpcap_adc_phasing_tbl *phase_tbl = req->phase_tbl;
  614. int index = req->channel;
  615. /* Remuxed channels 16 and 17 use BATTP and BATTI entries */
  616. switch (req->channel) {
  617. case CPCAP_ADC_BATTP:
  618. case CPCAP_ADC_BATTP_PI16:
  619. index = req->bank_index;
  620. req->result -= phase_tbl[index].offset;
  621. req->result -= CPCAP_FOUR_POINT_TWO_ADC;
  622. req->result *= phase_tbl[index].multiplier;
  623. if (phase_tbl[index].divider == 0)
  624. return;
  625. req->result /= phase_tbl[index].divider;
  626. req->result += CPCAP_FOUR_POINT_TWO_ADC;
  627. break;
  628. case CPCAP_ADC_BATTI_PI17:
  629. index = req->bank_index;
  630. /* fallthrough */
  631. default:
  632. req->result += conv_tbl[index].cal_offset;
  633. req->result += conv_tbl[index].align_offset;
  634. req->result *= phase_tbl[index].multiplier;
  635. if (phase_tbl[index].divider == 0)
  636. return;
  637. req->result /= phase_tbl[index].divider;
  638. req->result += phase_tbl[index].offset;
  639. break;
  640. }
  641. if (req->result < phase_tbl[index].min)
  642. req->result = phase_tbl[index].min;
  643. else if (req->result > phase_tbl[index].max)
  644. req->result = phase_tbl[index].max;
  645. }
  646. /* Looks up temperatures in a table and calculates averages if needed */
  647. static int cpcap_adc_table_to_millicelcius(unsigned short value)
  648. {
  649. int i, result = 0, alpha;
  650. if (value <= temp_map[CPCAP_MAX_TEMP_LVL - 1][0])
  651. return temp_map[CPCAP_MAX_TEMP_LVL - 1][1];
  652. if (value >= temp_map[0][0])
  653. return temp_map[0][1];
  654. for (i = 0; i < CPCAP_MAX_TEMP_LVL - 1; i++) {
  655. if ((value <= temp_map[i][0]) &&
  656. (value >= temp_map[i + 1][0])) {
  657. if (value == temp_map[i][0]) {
  658. result = temp_map[i][1];
  659. } else if (value == temp_map[i + 1][0]) {
  660. result = temp_map[i + 1][1];
  661. } else {
  662. alpha = ((value - temp_map[i][0]) * 1000) /
  663. (temp_map[i + 1][0] - temp_map[i][0]);
  664. result = temp_map[i][1] +
  665. ((alpha * (temp_map[i + 1][1] -
  666. temp_map[i][1])) / 1000);
  667. }
  668. break;
  669. }
  670. }
  671. return result;
  672. }
  673. static void cpcap_adc_convert(struct cpcap_adc_request *req)
  674. {
  675. const struct cpcap_adc_conversion_tbl *conv_tbl = req->conv_tbl;
  676. int index = req->channel;
  677. /* Remuxed channels 16 and 17 use BATTP and BATTI entries */
  678. switch (req->channel) {
  679. case CPCAP_ADC_BATTP_PI16:
  680. index = CPCAP_ADC_BATTP;
  681. break;
  682. case CPCAP_ADC_BATTI_PI17:
  683. index = CPCAP_ADC_BATTI;
  684. break;
  685. default:
  686. break;
  687. }
  688. /* No conversion for raw channels */
  689. if (conv_tbl[index].conv_type == IIO_CHAN_INFO_RAW)
  690. return;
  691. /* Temperatures use a lookup table instead of conversion table */
  692. if ((req->channel == CPCAP_ADC_AD0) ||
  693. (req->channel == CPCAP_ADC_AD3)) {
  694. req->result =
  695. cpcap_adc_table_to_millicelcius(req->result);
  696. return;
  697. }
  698. /* All processed channels use a conversion table */
  699. req->result *= conv_tbl[index].multiplier;
  700. if (conv_tbl[index].divider == 0)
  701. return;
  702. req->result /= conv_tbl[index].divider;
  703. req->result += conv_tbl[index].conv_offset;
  704. }
  705. /*
  706. * REVISIT: Check if timed sampling can use multiple channels at the
  707. * same time. If not, replace channel_mask with just channel.
  708. */
  709. static int cpcap_adc_read_bank_scaled(struct cpcap_adc *ddata,
  710. struct cpcap_adc_request *req)
  711. {
  712. int calibration_data, error, addr;
  713. if (ddata->vendor == CPCAP_VENDOR_TI) {
  714. error = regmap_read(ddata->reg, CPCAP_REG_ADCAL1,
  715. &calibration_data);
  716. if (error)
  717. return error;
  718. bank_conversion[CPCAP_ADC_CHG_ISENSE].cal_offset =
  719. ((short)calibration_data * -1) + 512;
  720. error = regmap_read(ddata->reg, CPCAP_REG_ADCAL2,
  721. &calibration_data);
  722. if (error)
  723. return error;
  724. bank_conversion[CPCAP_ADC_BATTI].cal_offset =
  725. ((short)calibration_data * -1) + 512;
  726. }
  727. addr = CPCAP_REG_ADCD0 + req->bank_index * 4;
  728. error = regmap_read(ddata->reg, addr, &req->result);
  729. if (error)
  730. return error;
  731. req->result &= 0x3ff;
  732. cpcap_adc_phase(req);
  733. cpcap_adc_convert(req);
  734. return 0;
  735. }
  736. static int cpcap_adc_init_request(struct cpcap_adc_request *req,
  737. int channel)
  738. {
  739. req->channel = channel;
  740. req->phase_tbl = bank_phasing;
  741. req->conv_tbl = bank_conversion;
  742. switch (channel) {
  743. case CPCAP_ADC_AD0 ... CPCAP_ADC_USB_ID:
  744. req->bank_index = channel;
  745. break;
  746. case CPCAP_ADC_AD8 ... CPCAP_ADC_TSY2_AD15:
  747. req->bank_index = channel - 8;
  748. break;
  749. case CPCAP_ADC_BATTP_PI16:
  750. req->bank_index = CPCAP_ADC_BATTP;
  751. break;
  752. case CPCAP_ADC_BATTI_PI17:
  753. req->bank_index = CPCAP_ADC_BATTI;
  754. break;
  755. default:
  756. return -EINVAL;
  757. }
  758. return 0;
  759. }
  760. static int cpcap_adc_read_st_die_temp(struct cpcap_adc *ddata,
  761. int addr, int *val)
  762. {
  763. int error;
  764. error = regmap_read(ddata->reg, addr, val);
  765. if (error)
  766. return error;
  767. *val -= 282;
  768. *val *= 114;
  769. *val += 25000;
  770. return 0;
  771. }
  772. static int cpcap_adc_read(struct iio_dev *indio_dev,
  773. struct iio_chan_spec const *chan,
  774. int *val, int *val2, long mask)
  775. {
  776. struct cpcap_adc *ddata = iio_priv(indio_dev);
  777. struct cpcap_adc_request req;
  778. int error;
  779. error = cpcap_adc_init_request(&req, chan->channel);
  780. if (error)
  781. return error;
  782. switch (mask) {
  783. case IIO_CHAN_INFO_RAW:
  784. mutex_lock(&ddata->lock);
  785. error = cpcap_adc_start_bank(ddata, &req);
  786. if (error)
  787. goto err_unlock;
  788. error = regmap_read(ddata->reg, chan->address, val);
  789. if (error)
  790. goto err_unlock;
  791. error = cpcap_adc_stop_bank(ddata);
  792. if (error)
  793. goto err_unlock;
  794. mutex_unlock(&ddata->lock);
  795. break;
  796. case IIO_CHAN_INFO_PROCESSED:
  797. mutex_lock(&ddata->lock);
  798. error = cpcap_adc_start_bank(ddata, &req);
  799. if (error)
  800. goto err_unlock;
  801. if ((ddata->vendor == CPCAP_VENDOR_ST) &&
  802. (chan->channel == CPCAP_ADC_AD3)) {
  803. error = cpcap_adc_read_st_die_temp(ddata,
  804. chan->address,
  805. &req.result);
  806. if (error)
  807. goto err_unlock;
  808. } else {
  809. error = cpcap_adc_read_bank_scaled(ddata, &req);
  810. if (error)
  811. goto err_unlock;
  812. }
  813. error = cpcap_adc_stop_bank(ddata);
  814. if (error)
  815. goto err_unlock;
  816. mutex_unlock(&ddata->lock);
  817. *val = req.result;
  818. break;
  819. default:
  820. return -EINVAL;
  821. }
  822. return IIO_VAL_INT;
  823. err_unlock:
  824. mutex_unlock(&ddata->lock);
  825. dev_err(ddata->dev, "error reading ADC: %i\n", error);
  826. return error;
  827. }
  828. static const struct iio_info cpcap_adc_info = {
  829. .read_raw = &cpcap_adc_read,
  830. };
  831. /*
  832. * Configuration for Motorola mapphone series such as droid 4.
  833. * Copied from the Motorola mapphone kernel tree.
  834. */
  835. static const struct cpcap_adc_ato mapphone_adc = {
  836. .ato_in = 0x0480,
  837. .atox_in = 0,
  838. .adc_ps_factor_in = 0x0200,
  839. .atox_ps_factor_in = 0,
  840. .ato_out = 0,
  841. .atox_out = 0,
  842. .adc_ps_factor_out = 0,
  843. .atox_ps_factor_out = 0,
  844. };
  845. static const struct of_device_id cpcap_adc_id_table[] = {
  846. {
  847. .compatible = "motorola,cpcap-adc",
  848. },
  849. {
  850. .compatible = "motorola,mapphone-cpcap-adc",
  851. .data = &mapphone_adc,
  852. },
  853. { /* sentinel */ },
  854. };
  855. MODULE_DEVICE_TABLE(of, cpcap_adc_id_table);
  856. static int cpcap_adc_probe(struct platform_device *pdev)
  857. {
  858. const struct of_device_id *match;
  859. struct cpcap_adc *ddata;
  860. struct iio_dev *indio_dev;
  861. int error;
  862. match = of_match_device(of_match_ptr(cpcap_adc_id_table),
  863. &pdev->dev);
  864. if (!match)
  865. return -EINVAL;
  866. if (!match->data) {
  867. dev_err(&pdev->dev, "no configuration data found\n");
  868. return -ENODEV;
  869. }
  870. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*ddata));
  871. if (!indio_dev) {
  872. dev_err(&pdev->dev, "failed to allocate iio device\n");
  873. return -ENOMEM;
  874. }
  875. ddata = iio_priv(indio_dev);
  876. ddata->ato = match->data;
  877. ddata->dev = &pdev->dev;
  878. mutex_init(&ddata->lock);
  879. init_waitqueue_head(&ddata->wq_data_avail);
  880. indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
  881. indio_dev->dev.parent = &pdev->dev;
  882. indio_dev->dev.of_node = pdev->dev.of_node;
  883. indio_dev->channels = cpcap_adc_channels;
  884. indio_dev->num_channels = ARRAY_SIZE(cpcap_adc_channels);
  885. indio_dev->name = dev_name(&pdev->dev);
  886. indio_dev->info = &cpcap_adc_info;
  887. ddata->reg = dev_get_regmap(pdev->dev.parent, NULL);
  888. if (!ddata->reg)
  889. return -ENODEV;
  890. error = cpcap_get_vendor(ddata->dev, ddata->reg, &ddata->vendor);
  891. if (error)
  892. return error;
  893. platform_set_drvdata(pdev, indio_dev);
  894. ddata->irq = platform_get_irq_byname(pdev, "adcdone");
  895. if (ddata->irq < 0)
  896. return -ENODEV;
  897. error = devm_request_threaded_irq(&pdev->dev, ddata->irq, NULL,
  898. cpcap_adc_irq_thread,
  899. IRQF_TRIGGER_NONE,
  900. "cpcap-adc", indio_dev);
  901. if (error) {
  902. dev_err(&pdev->dev, "could not get irq: %i\n",
  903. error);
  904. return error;
  905. }
  906. error = cpcap_adc_calibrate(ddata);
  907. if (error)
  908. return error;
  909. dev_info(&pdev->dev, "CPCAP ADC device probed\n");
  910. return devm_iio_device_register(&pdev->dev, indio_dev);
  911. }
  912. static struct platform_driver cpcap_adc_driver = {
  913. .driver = {
  914. .name = "cpcap_adc",
  915. .of_match_table = of_match_ptr(cpcap_adc_id_table),
  916. },
  917. .probe = cpcap_adc_probe,
  918. };
  919. module_platform_driver(cpcap_adc_driver);
  920. MODULE_ALIAS("platform:cpcap_adc");
  921. MODULE_DESCRIPTION("CPCAP ADC driver");
  922. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com");
  923. MODULE_LICENSE("GPL v2");