pmic8058-xoadc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/mutex.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/err.h>
  19. #include <linux/msm_adc.h>
  20. #include <linux/mfd/pm8xxx/core.h>
  21. #include <linux/mfd/pmic8058.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/slab.h>
  24. #include <linux/ratelimit.h>
  25. #include <linux/delay.h>
  26. #include <linux/wakelock.h>
  27. #include <mach/mpp.h>
  28. #include <mach/msm_xo.h>
  29. #define ADC_DRIVER_NAME "pm8058-xoadc"
  30. #define MAX_QUEUE_LENGTH 0X15
  31. #define MAX_CHANNEL_PROPERTIES_QUEUE 0X7
  32. #define MAX_QUEUE_SLOT 0x1
  33. /* User Processor */
  34. #define ADC_ARB_USRP_CNTRL 0x197
  35. #define ADC_ARB_USRP_CNTRL_EN_ARB BIT(0)
  36. #define ADC_ARB_USRP_CNTRL_RSV1 BIT(1)
  37. #define ADC_ARB_USRP_CNTRL_RSV2 BIT(2)
  38. #define ADC_ARB_USRP_CNTRL_RSV3 BIT(3)
  39. #define ADC_ARB_USRP_CNTRL_RSV4 BIT(4)
  40. #define ADC_ARB_USRP_CNTRL_RSV5 BIT(5)
  41. #define ADC_ARB_USRP_CNTRL_EOC BIT(6)
  42. #define ADC_ARB_USRP_CNTRL_REQ BIT(7)
  43. #define ADC_ARB_USRP_AMUX_CNTRL 0x198
  44. #define ADC_ARB_USRP_ANA_PARAM 0x199
  45. #define ADC_ARB_USRP_DIG_PARAM 0x19A
  46. #define ADC_ARB_USRP_RSV 0x19B
  47. #define ADC_ARB_USRP_DATA0 0x19D
  48. #define ADC_ARB_USRP_DATA1 0x19C
  49. struct pmic8058_adc {
  50. struct device *dev;
  51. struct xoadc_platform_data *pdata;
  52. struct adc_properties *adc_prop;
  53. struct xoadc_conv_state conv[2];
  54. int xoadc_queue_count;
  55. int adc_irq;
  56. struct linear_graph *adc_graph;
  57. struct xoadc_conv_state *conv_slot_request;
  58. struct xoadc_conv_state *conv_queue_list;
  59. struct adc_conv_slot conv_queue_elements[MAX_QUEUE_LENGTH];
  60. int xoadc_num;
  61. struct msm_xo_voter *adc_voter;
  62. struct wake_lock adc_wakelock;
  63. /* flag to warn/bug if wakelocks are taken after suspend_noirq */
  64. int msm_suspend_check;
  65. };
  66. static struct pmic8058_adc *pmic_adc[XOADC_PMIC_0 + 1];
  67. static bool xoadc_initialized, xoadc_calib_first_adc;
  68. DEFINE_RATELIMIT_STATE(pm8058_xoadc_msg_ratelimit,
  69. DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
  70. static inline int pm8058_xoadc_can_print(void)
  71. {
  72. return __ratelimit(&pm8058_xoadc_msg_ratelimit);
  73. }
  74. int32_t pm8058_xoadc_registered(void)
  75. {
  76. return xoadc_initialized;
  77. }
  78. EXPORT_SYMBOL(pm8058_xoadc_registered);
  79. void pm8058_xoadc_restore_slot(uint32_t adc_instance,
  80. struct adc_conv_slot *slot)
  81. {
  82. struct pmic8058_adc *adc_pmic = pmic_adc[adc_instance];
  83. struct xoadc_conv_state *slot_state = adc_pmic->conv_slot_request;
  84. mutex_lock(&slot_state->list_lock);
  85. list_add(&slot->list, &slot_state->slots);
  86. mutex_unlock(&slot_state->list_lock);
  87. }
  88. EXPORT_SYMBOL(pm8058_xoadc_restore_slot);
  89. void pm8058_xoadc_slot_request(uint32_t adc_instance,
  90. struct adc_conv_slot **slot)
  91. {
  92. struct pmic8058_adc *adc_pmic = pmic_adc[adc_instance];
  93. struct xoadc_conv_state *slot_state = adc_pmic->conv_slot_request;
  94. mutex_lock(&slot_state->list_lock);
  95. if (!list_empty(&slot_state->slots)) {
  96. *slot = list_first_entry(&slot_state->slots,
  97. struct adc_conv_slot, list);
  98. list_del(&(*slot)->list);
  99. } else
  100. *slot = NULL;
  101. mutex_unlock(&slot_state->list_lock);
  102. }
  103. EXPORT_SYMBOL(pm8058_xoadc_slot_request);
  104. static int32_t pm8058_xoadc_arb_cntrl(uint32_t arb_cntrl,
  105. uint32_t adc_instance, uint32_t channel)
  106. {
  107. struct pmic8058_adc *adc_pmic = pmic_adc[adc_instance];
  108. int i, rc;
  109. u8 data_arb_cntrl;
  110. data_arb_cntrl = ADC_ARB_USRP_CNTRL_EOC |
  111. ADC_ARB_USRP_CNTRL_RSV5 |
  112. ADC_ARB_USRP_CNTRL_RSV4;
  113. if (arb_cntrl) {
  114. if (adc_pmic->msm_suspend_check)
  115. pr_err("XOADC request being made after suspend irq "
  116. "with channel id:%d\n", channel);
  117. data_arb_cntrl |= ADC_ARB_USRP_CNTRL_EN_ARB;
  118. msm_xo_mode_vote(adc_pmic->adc_voter, MSM_XO_MODE_ON);
  119. adc_pmic->pdata->xoadc_mpp_config();
  120. wake_lock(&adc_pmic->adc_wakelock);
  121. }
  122. /* Write twice to the CNTRL register for the arbiter settings
  123. to take into effect */
  124. for (i = 0; i < 2; i++) {
  125. rc = pm8xxx_writeb(adc_pmic->dev->parent, ADC_ARB_USRP_CNTRL,
  126. data_arb_cntrl);
  127. if (rc < 0) {
  128. pr_debug("%s: PM8058 write failed\n", __func__);
  129. return rc;
  130. }
  131. }
  132. if (!arb_cntrl) {
  133. msm_xo_mode_vote(adc_pmic->adc_voter, MSM_XO_MODE_OFF);
  134. wake_unlock(&adc_pmic->adc_wakelock);
  135. }
  136. return 0;
  137. }
  138. static int32_t pm8058_xoadc_configure(uint32_t adc_instance,
  139. struct adc_conv_slot *slot)
  140. {
  141. struct pmic8058_adc *adc_pmic = pmic_adc[adc_instance];
  142. u8 data_arb_cntrl = 0, data_amux_chan = 0, data_arb_rsv = 0;
  143. u8 data_dig_param = 0, data_ana_param2 = 0, data_ana_param = 0;
  144. int rc;
  145. rc = pm8058_xoadc_arb_cntrl(1, adc_instance, slot->chan_path);
  146. if (rc < 0) {
  147. pr_debug("%s: Configuring ADC Arbiter"
  148. "enable failed\n", __func__);
  149. return rc;
  150. }
  151. switch (slot->chan_path) {
  152. case CHAN_PATH_TYPE1:
  153. data_amux_chan = CHANNEL_VCOIN << 4;
  154. data_arb_rsv = 0x20;
  155. slot->chan_properties.gain_numerator = 1;
  156. slot->chan_properties.gain_denominator = 2;
  157. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  158. break;
  159. case CHAN_PATH_TYPE2:
  160. data_amux_chan = CHANNEL_VBAT << 4;
  161. data_arb_rsv = 0x20;
  162. slot->chan_properties.gain_numerator = 1;
  163. slot->chan_properties.gain_denominator = 3;
  164. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  165. break;
  166. case CHAN_PATH_TYPE3:
  167. data_amux_chan = CHANNEL_VCHG << 4;
  168. data_arb_rsv = 0x20;
  169. slot->chan_properties.gain_numerator = 1;
  170. slot->chan_properties.gain_denominator = 10;
  171. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  172. break;
  173. case CHAN_PATH_TYPE4:
  174. data_amux_chan = CHANNEL_CHG_MONITOR << 4;
  175. data_arb_rsv = 0x20;
  176. slot->chan_properties.gain_numerator = 1;
  177. slot->chan_properties.gain_denominator = 1;
  178. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  179. break;
  180. case CHAN_PATH_TYPE5:
  181. data_amux_chan = CHANNEL_VPH_PWR << 4;
  182. data_arb_rsv = 0x20;
  183. slot->chan_properties.gain_numerator = 1;
  184. slot->chan_properties.gain_denominator = 3;
  185. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  186. break;
  187. case CHAN_PATH_TYPE6:
  188. data_amux_chan = CHANNEL_MPP5 << 4;
  189. data_arb_rsv = 0x20;
  190. slot->chan_properties.gain_numerator = 1;
  191. slot->chan_properties.gain_denominator = 1;
  192. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[1];
  193. break;
  194. case CHAN_PATH_TYPE7:
  195. data_amux_chan = CHANNEL_MPP6 << 4;
  196. data_arb_rsv = 0x20;
  197. slot->chan_properties.gain_numerator = 1;
  198. slot->chan_properties.gain_denominator = 1;
  199. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  200. break;
  201. case CHAN_PATH_TYPE8:
  202. data_amux_chan = CHANNEL_MPP7 << 4;
  203. data_arb_rsv = 0x20;
  204. slot->chan_properties.gain_numerator = 1;
  205. slot->chan_properties.gain_denominator = 2;
  206. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  207. break;
  208. case CHAN_PATH_TYPE9:
  209. data_amux_chan = CHANNEL_MPP8 << 4;
  210. data_arb_rsv = 0x20;
  211. slot->chan_properties.gain_numerator = 1;
  212. slot->chan_properties.gain_denominator = 2;
  213. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  214. break;
  215. case CHAN_PATH_TYPE10:
  216. data_amux_chan = CHANNEL_MPP9 << 4;
  217. data_arb_rsv = 0x20;
  218. slot->chan_properties.gain_numerator = 1;
  219. slot->chan_properties.gain_denominator = 3;
  220. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  221. break;
  222. case CHAN_PATH_TYPE11:
  223. data_amux_chan = CHANNEL_USB_VBUS << 4;
  224. data_arb_rsv = 0x20;
  225. slot->chan_properties.gain_numerator = 1;
  226. slot->chan_properties.gain_denominator = 3;
  227. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  228. break;
  229. case CHAN_PATH_TYPE12:
  230. data_amux_chan = CHANNEL_DIE_TEMP << 4;
  231. data_arb_rsv = 0x20;
  232. slot->chan_properties.gain_numerator = 1;
  233. slot->chan_properties.gain_denominator = 1;
  234. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  235. break;
  236. case CHAN_PATH_TYPE13:
  237. data_amux_chan = CHANNEL_125V << 4;
  238. data_arb_rsv = 0x20;
  239. slot->chan_properties.gain_numerator = 1;
  240. slot->chan_properties.gain_denominator = 1;
  241. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  242. break;
  243. case CHAN_PATH_TYPE14:
  244. data_amux_chan = CHANNEL_INTERNAL_2 << 4;
  245. data_arb_rsv = 0x20;
  246. slot->chan_properties.gain_numerator = 1;
  247. slot->chan_properties.gain_denominator = 1;
  248. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  249. break;
  250. case CHAN_PATH_TYPE_NONE:
  251. data_amux_chan = CHANNEL_MUXOFF << 4;
  252. data_arb_rsv = 0x10;
  253. slot->chan_properties.gain_numerator = 1;
  254. slot->chan_properties.gain_denominator = 1;
  255. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[1];
  256. break;
  257. case CHAN_PATH_TYPE15:
  258. data_amux_chan = CHANNEL_INTERNAL << 4;
  259. data_arb_rsv = 0x20;
  260. slot->chan_properties.gain_numerator = 1;
  261. slot->chan_properties.gain_denominator = 1;
  262. slot->chan_properties.adc_graph = &adc_pmic->adc_graph[0];
  263. break;
  264. }
  265. rc = pm8xxx_writeb(adc_pmic->dev->parent,
  266. ADC_ARB_USRP_AMUX_CNTRL, data_amux_chan);
  267. if (rc < 0) {
  268. pr_debug("%s: PM8058 write failed\n", __func__);
  269. return rc;
  270. }
  271. rc = pm8xxx_writeb(adc_pmic->dev->parent,
  272. ADC_ARB_USRP_RSV, data_arb_rsv);
  273. if (rc < 0) {
  274. pr_debug("%s: PM8058 write failed\n", __func__);
  275. return rc;
  276. }
  277. /* Set default clock rate to 2.4 MHz XO ADC clock digital */
  278. switch (slot->chan_adc_config) {
  279. case ADC_CONFIG_TYPE1:
  280. data_ana_param = 0xFE;
  281. data_dig_param = 0x23;
  282. data_ana_param2 = 0xFF;
  283. /* AMUX register data to start the ADC conversion */
  284. data_arb_cntrl = 0xF1;
  285. break;
  286. case ADC_CONFIG_TYPE2:
  287. data_ana_param = 0xFE;
  288. data_dig_param = 0x03;
  289. data_ana_param2 = 0xFF;
  290. /* AMUX register data to start the ADC conversion */
  291. data_arb_cntrl = 0xF1;
  292. break;
  293. }
  294. rc = pm8xxx_writeb(adc_pmic->dev->parent,
  295. ADC_ARB_USRP_ANA_PARAM, data_ana_param);
  296. if (rc < 0) {
  297. pr_debug("%s: PM8058 write failed\n", __func__);
  298. return rc;
  299. }
  300. rc = pm8xxx_writeb(adc_pmic->dev->parent,
  301. ADC_ARB_USRP_DIG_PARAM, data_dig_param);
  302. if (rc < 0) {
  303. pr_debug("%s: PM8058 write failed\n", __func__);
  304. return rc;
  305. }
  306. rc = pm8xxx_writeb(adc_pmic->dev->parent,
  307. ADC_ARB_USRP_ANA_PARAM, data_ana_param2);
  308. if (rc < 0) {
  309. pr_debug("%s: PM8058 write failed\n", __func__);
  310. return rc;
  311. }
  312. enable_irq(adc_pmic->adc_irq);
  313. rc = pm8xxx_writeb(adc_pmic->dev->parent,
  314. ADC_ARB_USRP_CNTRL, data_arb_cntrl);
  315. if (rc < 0) {
  316. pr_debug("%s: PM8058 write failed\n", __func__);
  317. return rc;
  318. }
  319. return 0;
  320. }
  321. int32_t pm8058_xoadc_select_chan_and_start_conv(uint32_t adc_instance,
  322. struct adc_conv_slot *slot)
  323. {
  324. struct pmic8058_adc *adc_pmic = pmic_adc[adc_instance];
  325. struct xoadc_conv_state *slot_state = adc_pmic->conv_queue_list;
  326. if (!xoadc_initialized)
  327. return -ENODEV;
  328. mutex_lock(&slot_state->list_lock);
  329. list_add_tail(&slot->list, &slot_state->slots);
  330. if (adc_pmic->xoadc_queue_count == 0) {
  331. if (adc_pmic->pdata->xoadc_vreg_set != NULL)
  332. adc_pmic->pdata->xoadc_vreg_set(1);
  333. pm8058_xoadc_configure(adc_instance, slot);
  334. }
  335. adc_pmic->xoadc_queue_count++;
  336. mutex_unlock(&slot_state->list_lock);
  337. return 0;
  338. }
  339. EXPORT_SYMBOL(pm8058_xoadc_select_chan_and_start_conv);
  340. static int32_t pm8058_xoadc_dequeue_slot_request(uint32_t adc_instance,
  341. struct adc_conv_slot **slot)
  342. {
  343. struct pmic8058_adc *adc_pmic = pmic_adc[adc_instance];
  344. struct xoadc_conv_state *slot_state = adc_pmic->conv_queue_list;
  345. int rc = 0;
  346. mutex_lock(&slot_state->list_lock);
  347. if (adc_pmic->xoadc_queue_count > 0 &&
  348. !list_empty(&slot_state->slots)) {
  349. *slot = list_first_entry(&slot_state->slots,
  350. struct adc_conv_slot, list);
  351. list_del(&(*slot)->list);
  352. } else
  353. rc = -EINVAL;
  354. mutex_unlock(&slot_state->list_lock);
  355. if (rc < 0) {
  356. if (pm8058_xoadc_can_print())
  357. pr_err("Pmic 8058 xoadc spurious interrupt detected\n");
  358. return rc;
  359. }
  360. return 0;
  361. }
  362. int32_t pm8058_xoadc_read_adc_code(uint32_t adc_instance, int32_t *data)
  363. {
  364. struct pmic8058_adc *adc_pmic = pmic_adc[adc_instance];
  365. struct xoadc_conv_state *slot_state = adc_pmic->conv_queue_list;
  366. uint8_t rslt_lsb, rslt_msb;
  367. struct adc_conv_slot *slot;
  368. int32_t rc, max_ideal_adc_code = 1 << adc_pmic->adc_prop->bitresolution;
  369. if (!xoadc_initialized)
  370. return -ENODEV;
  371. rc = pm8xxx_readb(adc_pmic->dev->parent, ADC_ARB_USRP_DATA0,
  372. &rslt_lsb);
  373. if (rc < 0) {
  374. pr_debug("%s: PM8058 read failed\n", __func__);
  375. return rc;
  376. }
  377. rc = pm8xxx_readb(adc_pmic->dev->parent, ADC_ARB_USRP_DATA1,
  378. &rslt_msb);
  379. if (rc < 0) {
  380. pr_debug("%s: PM8058 read failed\n", __func__);
  381. return rc;
  382. }
  383. *data = (rslt_msb << 8) | rslt_lsb;
  384. /* Use the midpoint to determine underflow or overflow */
  385. if (*data > max_ideal_adc_code + (max_ideal_adc_code >> 1))
  386. *data |= ((1 << (8 * sizeof(*data) -
  387. adc_pmic->adc_prop->bitresolution)) - 1) <<
  388. adc_pmic->adc_prop->bitresolution;
  389. /* Return if this is a calibration run since there
  390. * is no need to check requests in the waiting queue */
  391. if (xoadc_calib_first_adc)
  392. return 0;
  393. mutex_lock(&slot_state->list_lock);
  394. adc_pmic->xoadc_queue_count--;
  395. if (adc_pmic->xoadc_queue_count > 0) {
  396. slot = list_first_entry(&slot_state->slots,
  397. struct adc_conv_slot, list);
  398. pm8058_xoadc_configure(adc_instance, slot);
  399. }
  400. mutex_unlock(&slot_state->list_lock);
  401. mutex_lock(&slot_state->list_lock);
  402. /* Default value for switching off the arbiter after reading
  403. the ADC value. Bit 0 set to 0. */
  404. if (adc_pmic->xoadc_queue_count == 0) {
  405. rc = pm8058_xoadc_arb_cntrl(0, adc_instance, CHANNEL_MUXOFF);
  406. if (rc < 0) {
  407. pr_debug("%s: Configuring ADC Arbiter disable"
  408. "failed\n", __func__);
  409. return rc;
  410. }
  411. if (adc_pmic->pdata->xoadc_vreg_set != NULL)
  412. adc_pmic->pdata->xoadc_vreg_set(0);
  413. }
  414. mutex_unlock(&slot_state->list_lock);
  415. return 0;
  416. }
  417. EXPORT_SYMBOL(pm8058_xoadc_read_adc_code);
  418. static irqreturn_t pm8058_xoadc(int irq, void *dev_id)
  419. {
  420. struct pmic8058_adc *xoadc_8058 = dev_id;
  421. struct adc_conv_slot *slot = NULL;
  422. int rc;
  423. disable_irq_nosync(xoadc_8058->adc_irq);
  424. if (xoadc_calib_first_adc)
  425. return IRQ_HANDLED;
  426. rc = pm8058_xoadc_dequeue_slot_request(xoadc_8058->xoadc_num, &slot);
  427. if (rc < 0)
  428. return IRQ_NONE;
  429. if (rc == 0)
  430. msm_adc_conv_cb(slot, 0, NULL, 0);
  431. return IRQ_HANDLED;
  432. }
  433. struct adc_properties *pm8058_xoadc_get_properties(uint32_t dev_instance)
  434. {
  435. struct pmic8058_adc *xoadc_8058 = pmic_adc[dev_instance];
  436. return xoadc_8058->adc_prop;
  437. }
  438. EXPORT_SYMBOL(pm8058_xoadc_get_properties);
  439. int32_t pm8058_xoadc_calib_device(uint32_t adc_instance)
  440. {
  441. struct pmic8058_adc *adc_pmic = pmic_adc[adc_instance];
  442. struct adc_conv_slot *slot;
  443. int rc, offset_xoadc, slope_xoadc, calib_read_1, calib_read_2;
  444. if (adc_pmic->pdata->xoadc_vreg_set != NULL)
  445. adc_pmic->pdata->xoadc_vreg_set(1);
  446. pm8058_xoadc_slot_request(adc_instance, &slot);
  447. if (slot) {
  448. slot->chan_path = CHAN_PATH_TYPE13;
  449. slot->chan_adc_config = ADC_CONFIG_TYPE2;
  450. slot->chan_adc_calib = ADC_CONFIG_TYPE2;
  451. xoadc_calib_first_adc = true;
  452. rc = pm8058_xoadc_configure(adc_instance, slot);
  453. if (rc) {
  454. pr_err("pm8058_xoadc configure failed\n");
  455. goto fail;
  456. }
  457. } else {
  458. rc = -EINVAL;
  459. goto fail;
  460. }
  461. msleep(3);
  462. rc = pm8058_xoadc_read_adc_code(adc_instance, &calib_read_1);
  463. if (rc) {
  464. pr_err("pm8058_xoadc read adc failed\n");
  465. xoadc_calib_first_adc = false;
  466. goto fail;
  467. }
  468. xoadc_calib_first_adc = false;
  469. pm8058_xoadc_slot_request(adc_instance, &slot);
  470. if (slot) {
  471. slot->chan_path = CHAN_PATH_TYPE15;
  472. slot->chan_adc_config = ADC_CONFIG_TYPE2;
  473. slot->chan_adc_calib = ADC_CONFIG_TYPE2;
  474. xoadc_calib_first_adc = true;
  475. rc = pm8058_xoadc_configure(adc_instance, slot);
  476. if (rc) {
  477. pr_err("pm8058_xoadc configure failed\n");
  478. goto fail;
  479. }
  480. } else {
  481. rc = -EINVAL;
  482. goto fail;
  483. }
  484. msleep(3);
  485. rc = pm8058_xoadc_read_adc_code(adc_instance, &calib_read_2);
  486. if (rc) {
  487. pr_err("pm8058_xoadc read adc failed\n");
  488. xoadc_calib_first_adc = false;
  489. goto fail;
  490. }
  491. xoadc_calib_first_adc = false;
  492. pm8058_xoadc_restore_slot(adc_instance, slot);
  493. slope_xoadc = (((calib_read_1 - calib_read_2) << 10)/
  494. CHANNEL_ADC_625_MV);
  495. offset_xoadc = calib_read_2 -
  496. ((slope_xoadc * CHANNEL_ADC_625_MV) >> 10);
  497. printk(KERN_INFO"pmic8058_xoadc:The offset for AMUX calibration"
  498. "was %d\n", offset_xoadc);
  499. adc_pmic->adc_graph[0].offset = offset_xoadc;
  500. adc_pmic->adc_graph[0].dy = (calib_read_1 - calib_read_2);
  501. adc_pmic->adc_graph[0].dx = CHANNEL_ADC_625_MV;
  502. /* Retain ideal calibration settings for therm readings */
  503. adc_pmic->adc_graph[1].offset = 0 ;
  504. adc_pmic->adc_graph[1].dy = (1 << 15) - 1;
  505. adc_pmic->adc_graph[1].dx = 2200;
  506. if (adc_pmic->pdata->xoadc_vreg_set != NULL)
  507. adc_pmic->pdata->xoadc_vreg_set(0);
  508. return 0;
  509. fail:
  510. if (adc_pmic->pdata->xoadc_vreg_set != NULL)
  511. adc_pmic->pdata->xoadc_vreg_set(0);
  512. return rc;
  513. }
  514. EXPORT_SYMBOL(pm8058_xoadc_calib_device);
  515. int32_t pm8058_xoadc_calibrate(uint32_t dev_instance,
  516. struct adc_conv_slot *slot, int *calib_status)
  517. {
  518. *calib_status = CALIB_NOT_REQUIRED;
  519. return 0;
  520. }
  521. EXPORT_SYMBOL(pm8058_xoadc_calibrate);
  522. #ifdef CONFIG_PM
  523. static int pm8058_xoadc_suspend_noirq(struct device *dev)
  524. {
  525. struct platform_device *pdev = to_platform_device(dev);
  526. struct pmic8058_adc *adc_pmic = platform_get_drvdata(pdev);
  527. adc_pmic->msm_suspend_check = 1;
  528. return 0;
  529. }
  530. static int pm8058_xoadc_resume_noirq(struct device *dev)
  531. {
  532. struct platform_device *pdev = to_platform_device(dev);
  533. struct pmic8058_adc *adc_pmic = platform_get_drvdata(pdev);
  534. adc_pmic->msm_suspend_check = 0;
  535. return 0;
  536. }
  537. static const struct dev_pm_ops pm8058_xoadc_dev_pm_ops = {
  538. .suspend_noirq = pm8058_xoadc_suspend_noirq,
  539. .resume_noirq = pm8058_xoadc_resume_noirq,
  540. };
  541. #define PM8058_XOADC_DEV_PM_OPS (&pm8058_xoadc_dev_pm_ops)
  542. #else
  543. #define PM8058_XOADC_DEV_PM_OPS NULL
  544. #endif
  545. static int __devinit pm8058_xoadc_probe(struct platform_device *pdev)
  546. {
  547. struct xoadc_platform_data *pdata = pdev->dev.platform_data;
  548. struct pmic8058_adc *adc_pmic;
  549. int i, rc = 0;
  550. if (!pdata) {
  551. dev_err(&pdev->dev, "no platform data?\n");
  552. return -EINVAL;
  553. }
  554. adc_pmic = devm_kzalloc(&pdev->dev, sizeof(*adc_pmic), GFP_KERNEL);
  555. if (!adc_pmic) {
  556. dev_err(&pdev->dev, "Unable to allocate memory\n");
  557. return -ENOMEM;
  558. }
  559. adc_pmic->dev = &pdev->dev;
  560. adc_pmic->adc_prop = pdata->xoadc_prop;
  561. adc_pmic->xoadc_num = pdata->xoadc_num;
  562. adc_pmic->xoadc_queue_count = 0;
  563. platform_set_drvdata(pdev, adc_pmic);
  564. if (adc_pmic->xoadc_num > XOADC_PMIC_0) {
  565. dev_err(&pdev->dev, "ADC device not supported\n");
  566. return -EINVAL;
  567. }
  568. adc_pmic->pdata = pdata;
  569. adc_pmic->adc_graph = devm_kzalloc(&pdev->dev,
  570. sizeof(struct linear_graph) * MAX_CHANNEL_PROPERTIES_QUEUE,
  571. GFP_KERNEL);
  572. if (!adc_pmic->adc_graph) {
  573. dev_err(&pdev->dev, "Unable to allocate memory\n");
  574. return -ENOMEM;
  575. }
  576. /* Will be replaced by individual channel calibration */
  577. for (i = 0; i < MAX_CHANNEL_PROPERTIES_QUEUE; i++) {
  578. adc_pmic->adc_graph[i].offset = 0 ;
  579. adc_pmic->adc_graph[i].dy = (1 << 15) - 1;
  580. adc_pmic->adc_graph[i].dx = 2200;
  581. }
  582. if (pdata->xoadc_mpp_config != NULL)
  583. pdata->xoadc_mpp_config();
  584. adc_pmic->conv_slot_request = &adc_pmic->conv[0];
  585. adc_pmic->conv_slot_request->context =
  586. &adc_pmic->conv_queue_elements[0];
  587. mutex_init(&adc_pmic->conv_slot_request->list_lock);
  588. INIT_LIST_HEAD(&adc_pmic->conv_slot_request->slots);
  589. /* tie each slot and initwork them */
  590. for (i = 0; i < MAX_QUEUE_LENGTH; i++) {
  591. list_add(&adc_pmic->conv_slot_request->context[i].list,
  592. &adc_pmic->conv_slot_request->slots);
  593. INIT_WORK(&adc_pmic->conv_slot_request->context[i].work,
  594. msm_adc_wq_work);
  595. init_completion(&adc_pmic->conv_slot_request->context[i].comp);
  596. adc_pmic->conv_slot_request->context[i].idx = i;
  597. }
  598. adc_pmic->conv_queue_list = &adc_pmic->conv[1];
  599. mutex_init(&adc_pmic->conv_queue_list->list_lock);
  600. INIT_LIST_HEAD(&adc_pmic->conv_queue_list->slots);
  601. adc_pmic->adc_irq = platform_get_irq(pdev, 0);
  602. if (adc_pmic->adc_irq < 0)
  603. return -ENXIO;
  604. rc = request_threaded_irq(adc_pmic->adc_irq,
  605. NULL, pm8058_xoadc,
  606. IRQF_TRIGGER_RISING, "pm8058_adc_interrupt", adc_pmic);
  607. if (rc) {
  608. dev_err(&pdev->dev, "failed to request adc irq\n");
  609. return rc;
  610. }
  611. disable_irq(adc_pmic->adc_irq);
  612. if (adc_pmic->adc_voter == NULL) {
  613. adc_pmic->adc_voter = msm_xo_get(MSM_XO_TCXO_D1,
  614. "pmic8058_xoadc");
  615. if (IS_ERR(adc_pmic->adc_voter)) {
  616. dev_err(&pdev->dev, "Failed to get XO vote\n");
  617. return PTR_ERR(adc_pmic->adc_voter);
  618. }
  619. }
  620. device_init_wakeup(&pdev->dev, pdata->xoadc_wakeup);
  621. wake_lock_init(&adc_pmic->adc_wakelock, WAKE_LOCK_SUSPEND,
  622. "pmic8058_xoadc_wakelock");
  623. pmic_adc[adc_pmic->xoadc_num] = adc_pmic;
  624. if (pdata->xoadc_vreg_setup != NULL)
  625. pdata->xoadc_vreg_setup();
  626. xoadc_initialized = true;
  627. xoadc_calib_first_adc = false;
  628. return 0;
  629. }
  630. static int __devexit pm8058_xoadc_teardown(struct platform_device *pdev)
  631. {
  632. struct pmic8058_adc *adc_pmic = platform_get_drvdata(pdev);
  633. if (adc_pmic->pdata->xoadc_vreg_shutdown != NULL)
  634. adc_pmic->pdata->xoadc_vreg_shutdown();
  635. wake_lock_destroy(&adc_pmic->adc_wakelock);
  636. msm_xo_put(adc_pmic->adc_voter);
  637. device_init_wakeup(&pdev->dev, 0);
  638. xoadc_initialized = false;
  639. return 0;
  640. }
  641. static struct platform_driver pm8058_xoadc_driver = {
  642. .probe = pm8058_xoadc_probe,
  643. .remove = __devexit_p(pm8058_xoadc_teardown),
  644. .driver = {
  645. .name = "pm8058-xoadc",
  646. .owner = THIS_MODULE,
  647. .pm = PM8058_XOADC_DEV_PM_OPS,
  648. },
  649. };
  650. static int __init pm8058_xoadc_init(void)
  651. {
  652. return platform_driver_register(&pm8058_xoadc_driver);
  653. }
  654. module_init(pm8058_xoadc_init);
  655. static void __exit pm8058_xoadc_exit(void)
  656. {
  657. platform_driver_unregister(&pm8058_xoadc_driver);
  658. }
  659. module_exit(pm8058_xoadc_exit);
  660. MODULE_ALIAS("platform:pmic8058_xoadc");
  661. MODULE_DESCRIPTION("PMIC8058 XOADC driver");
  662. MODULE_LICENSE("GPL v2");