pm8xxx-adc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. /*
  2. * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * Qualcomm's PM8921/PM8018 ADC Arbiter driver
  14. */
  15. #define pr_fmt(fmt) "%s: " fmt, __func__
  16. #include <linux/kernel.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/delay.h>
  21. #include <linux/mutex.h>
  22. #include <linux/hwmon.h>
  23. #include <linux/module.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/completion.h>
  27. #include <linux/hwmon-sysfs.h>
  28. #include <linux/mfd/pm8xxx/mpp.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/mfd/pm8xxx/core.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/mfd/pm8xxx/pm8xxx-adc.h>
  33. #include <mach/msm_xo.h>
  34. /* User Bank register set */
  35. #define PM8XXX_ADC_ARB_USRP_CNTRL1 0x197
  36. #define PM8XXX_ADC_ARB_USRP_CNTRL1_EN_ARB BIT(0)
  37. #define PM8XXX_ADC_ARB_USRP_CNTRL1_RSV1 BIT(1)
  38. #define PM8XXX_ADC_ARB_USRP_CNTRL1_RSV2 BIT(2)
  39. #define PM8XXX_ADC_ARB_USRP_CNTRL1_RSV3 BIT(3)
  40. #define PM8XXX_ADC_ARB_USRP_CNTRL1_RSV4 BIT(4)
  41. #define PM8XXX_ADC_ARB_USRP_CNTRL1_RSV5 BIT(5)
  42. #define PM8XXX_ADC_ARB_USRP_CNTRL1_EOC BIT(6)
  43. #define PM8XXX_ADC_ARB_USRP_CNTRL1_REQ BIT(7)
  44. #define PM8XXX_ADC_ARB_USRP_AMUX_CNTRL 0x198
  45. #define PM8XXX_ADC_ARB_USRP_AMUX_CNTRL_RSV0 BIT(0)
  46. #define PM8XXX_ADC_ARB_USRP_AMUX_CNTRL_RSV1 BIT(1)
  47. #define PM8XXX_ADC_ARB_USRP_AMUX_CNTRL_PREMUX0 BIT(2)
  48. #define PM8XXX_ADC_ARB_USRP_AMUX_CNTRL_PREMUX1 BIT(3)
  49. #define PM8XXX_ADC_ARB_USRP_AMUX_CNTRL_SEL0 BIT(4)
  50. #define PM8XXX_ADC_ARB_USRP_AMUX_CNTRL_SEL1 BIT(5)
  51. #define PM8XXX_ADC_ARB_USRP_AMUX_CNTRL_SEL2 BIT(6)
  52. #define PM8XXX_ADC_ARB_USRP_AMUX_CNTRL_SEL3 BIT(7)
  53. #define PM8XXX_ADC_ARB_USRP_ANA_PARAM 0x199
  54. #define PM8XXX_ADC_ARB_USRP_DIG_PARAM 0x19A
  55. #define PM8XXX_ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT0 BIT(0)
  56. #define PM8XXX_ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT1 BIT(1)
  57. #define PM8XXX_ADC_ARB_USRP_DIG_PARAM_CLK_RATE0 BIT(2)
  58. #define PM8XXX_ADC_ARB_USRP_DIG_PARAM_CLK_RATE1 BIT(3)
  59. #define PM8XXX_ADC_ARB_USRP_DIG_PARAM_EOC BIT(4)
  60. #define PM8XXX_ADC_ARB_USRP_DIG_PARAM_DEC_RATE0 BIT(5)
  61. #define PM8XXX_ADC_ARB_USRP_DIG_PARAM_DEC_RATE1 BIT(6)
  62. #define PM8XXX_ADC_ARB_USRP_DIG_PARAM_EN BIT(7)
  63. #define PM8XXX_ADC_ARB_USRP_RSV 0x19B
  64. #define PM8XXX_ADC_ARB_USRP_RSV_RST BIT(0)
  65. #define PM8XXX_ADC_ARB_USRP_RSV_DTEST0 BIT(1)
  66. #define PM8XXX_ADC_ARB_USRP_RSV_DTEST1 BIT(2)
  67. #define PM8XXX_ADC_ARB_USRP_RSV_OP BIT(3)
  68. #define PM8XXX_ADC_ARB_USRP_RSV_IP_SEL0 BIT(4)
  69. #define PM8XXX_ADC_ARB_USRP_RSV_IP_SEL1 BIT(5)
  70. #define PM8XXX_ADC_ARB_USRP_RSV_IP_SEL2 BIT(6)
  71. #define PM8XXX_ADC_ARB_USRP_RSV_TRM BIT(7)
  72. #define PM8XXX_ADC_ARB_USRP_DATA0 0x19D
  73. #define PM8XXX_ADC_ARB_USRP_DATA1 0x19C
  74. #define PM8XXX_ADC_ARB_BTM_CNTRL1 0x17e
  75. #define PM8XXX_ADC_ARB_BTM_CNTRL1_EN_BTM BIT(0)
  76. #define PM8XXX_ADC_ARB_BTM_CNTRL1_SEL_OP_MODE BIT(1)
  77. #define PM8XXX_ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL1 BIT(2)
  78. #define PM8XXX_ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL2 BIT(3)
  79. #define PM8XXX_ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL3 BIT(4)
  80. #define PM8XXX_ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL4 BIT(5)
  81. #define PM8XXX_ADC_ARB_BTM_CNTRL1_EOC BIT(6)
  82. #define PM8XXX_ADC_ARB_BTM_CNTRL1_REQ BIT(7)
  83. #define PM8XXX_ADC_ARB_BTM_CNTRL2 0x18c
  84. #define PM8XXX_ADC_ARB_BTM_AMUX_CNTRL 0x17f
  85. #define PM8XXX_ADC_ARB_BTM_ANA_PARAM 0x180
  86. #define PM8XXX_ADC_ARB_BTM_DIG_PARAM 0x181
  87. #define PM8XXX_ADC_ARB_BTM_RSV 0x182
  88. #define PM8XXX_ADC_ARB_BTM_DATA1 0x183
  89. #define PM8XXX_ADC_ARB_BTM_DATA0 0x184
  90. #define PM8XXX_ADC_ARB_BTM_BAT_COOL_THR1 0x185
  91. #define PM8XXX_ADC_ARB_BTM_BAT_COOL_THR0 0x186
  92. #define PM8XXX_ADC_ARB_BTM_BAT_WARM_THR1 0x187
  93. #define PM8XXX_ADC_ARB_BTM_BAT_WARM_THR0 0x188
  94. #define PM8XXX_ADC_ARB_ANA_DIG 0xa0
  95. #define PM8XXX_ADC_BTM_RSV 0x10
  96. #define PM8XXX_ADC_AMUX_MPP_SEL 2
  97. #define PM8XXX_ADC_AMUX_SEL 4
  98. #define PM8XXX_ADC_RSV_IP_SEL 4
  99. #define PM8XXX_ADC_BTM_CHANNEL_SEL 4
  100. #define PM8XXX_MAX_CHANNEL_PROPERTIES 2
  101. #define PM8XXX_ADC_IRQ_0 0
  102. #define PM8XXX_ADC_IRQ_1 1
  103. #define PM8XXX_ADC_IRQ_2 2
  104. #define PM8XXX_ADC_BTM_INTERVAL_SEL_MASK 0xF
  105. #define PM8XXX_ADC_BTM_INTERVAL_SEL_SHIFT 2
  106. #define PM8XXX_ADC_BTM_DECIMATION_SEL 5
  107. #define PM8XXX_ADC_MUL 10
  108. #define PM8XXX_ADC_CONV_TIME_MIN 2000
  109. #define PM8XXX_ADC_CONV_TIME_MAX 2100
  110. #define PM8XXX_ADC_MPP_SETTLE_TIME_MIN 200
  111. #define PM8XXX_ADC_MPP_SETTLE_TIME_MAX 200
  112. #define PM8XXX_ADC_PA_THERM_VREG_UV_MIN 1800000
  113. #define PM8XXX_ADC_PA_THERM_VREG_UV_MAX 1800000
  114. #define PM8XXX_ADC_PA_THERM_VREG_UA_LOAD 100000
  115. #define PM8XXX_ADC_HWMON_NAME_LENGTH 32
  116. #define PM8XXX_ADC_BTM_INTERVAL_MAX 0x14
  117. #define PM8XXX_ADC_COMPLETION_TIMEOUT (2 * HZ)
  118. struct pm8xxx_adc {
  119. struct device *dev;
  120. struct pm8xxx_adc_properties *adc_prop;
  121. int adc_irq;
  122. struct mutex adc_lock;
  123. struct mutex mpp_adc_lock;
  124. spinlock_t btm_lock;
  125. uint32_t adc_num_board_channel;
  126. struct completion adc_rslt_completion;
  127. struct pm8xxx_adc_amux *adc_channel;
  128. int btm_warm_irq;
  129. int btm_cool_irq;
  130. struct dentry *dent;
  131. struct work_struct warm_work;
  132. struct work_struct cool_work;
  133. uint32_t mpp_base;
  134. struct device *hwmon;
  135. struct msm_xo_voter *adc_voter;
  136. int msm_suspend_check;
  137. struct pm8xxx_adc_amux_properties *conv;
  138. struct pm8xxx_adc_arb_btm_param batt;
  139. struct sensor_device_attribute sens_attr[0];
  140. };
  141. struct pm8xxx_adc_amux_properties {
  142. uint32_t amux_channel;
  143. uint32_t decimation;
  144. uint32_t amux_ip_rsv;
  145. uint32_t amux_mpp_channel;
  146. struct pm8xxx_adc_chan_properties chan_prop[0];
  147. };
  148. static const struct pm8xxx_adc_scaling_ratio pm8xxx_amux_scaling_ratio[] = {
  149. {1, 1},
  150. {1, 3},
  151. {1, 4},
  152. {1, 6}
  153. };
  154. static struct pm8xxx_adc *pmic_adc;
  155. static struct regulator *pa_therm;
  156. static struct pm8xxx_adc_scale_fn adc_scale_fn[] = {
  157. [ADC_SCALE_DEFAULT] = {pm8xxx_adc_scale_default},
  158. [ADC_SCALE_BATT_THERM] = {pm8xxx_adc_scale_batt_therm},
  159. [ADC_SCALE_PA_THERM] = {pm8xxx_adc_scale_pa_therm},
  160. [ADC_SCALE_PMIC_THERM] = {pm8xxx_adc_scale_pmic_therm},
  161. [ADC_SCALE_XOTHERM] = {pm8xxx_adc_tdkntcg_therm},
  162. };
  163. /* On PM8921 ADC the MPP needs to first be configured
  164. as an analog input to the AMUX pre-mux channel before
  165. issuing a read request. PM8921 MPP 8 is mapped to AMUX8
  166. and is common between remote processor's.
  167. On PM8018 ADC the MPP is directly connected to the AMUX
  168. pre-mux. Therefore clients of the PM8018 MPP do not need
  169. to configure the MPP as an analog input to the pre-mux.
  170. Clients can directly issue request on the pre-mux AMUX
  171. channel to read the ADC on the MPP */
  172. static struct pm8xxx_mpp_config_data pm8xxx_adc_mpp_config = {
  173. .type = PM8XXX_MPP_TYPE_A_INPUT,
  174. /* AMUX6 is dedicated to be used for apps processor */
  175. .level = PM8XXX_MPP_AIN_AMUX_CH6,
  176. .control = PM8XXX_MPP_AOUT_CTRL_DISABLE,
  177. };
  178. /* MPP Configuration for default settings */
  179. static struct pm8xxx_mpp_config_data pm8xxx_adc_mpp_unconfig = {
  180. .type = PM8XXX_MPP_TYPE_SINK,
  181. .level = PM8XXX_MPP_AIN_AMUX_CH5,
  182. .control = PM8XXX_MPP_AOUT_CTRL_DISABLE,
  183. };
  184. static bool pm8xxx_adc_calib_first_adc;
  185. static bool pm8xxx_adc_initialized, pm8xxx_adc_calib_device_init;
  186. static int32_t pm8xxx_adc_check_channel_valid(uint32_t channel)
  187. {
  188. if (channel < CHANNEL_VCOIN ||
  189. (channel > CHANNEL_MUXOFF && channel < ADC_MPP_1_ATEST_8) ||
  190. (channel > ADC_MPP_1_ATEST_7 && channel < ADC_MPP_2_ATEST_8)
  191. || (channel >= ADC_CHANNEL_MAX_NUM))
  192. return -EBADF;
  193. else
  194. return 0;
  195. }
  196. static int32_t pm8xxx_adc_arb_cntrl(uint32_t arb_cntrl,
  197. uint32_t channel)
  198. {
  199. struct pm8xxx_adc *adc_pmic = pmic_adc;
  200. int i, rc;
  201. u8 data_arb_cntrl = 0;
  202. if (arb_cntrl) {
  203. if (adc_pmic->msm_suspend_check)
  204. pr_err("PM8xxx ADC request made after suspend_noirq "
  205. "with channel: %d\n", channel);
  206. data_arb_cntrl |= PM8XXX_ADC_ARB_USRP_CNTRL1_EN_ARB;
  207. }
  208. /* Write twice to the CNTRL register for the arbiter settings
  209. to take into effect */
  210. for (i = 0; i < 2; i++) {
  211. rc = pm8xxx_writeb(adc_pmic->dev->parent,
  212. PM8XXX_ADC_ARB_USRP_CNTRL1, data_arb_cntrl);
  213. if (rc < 0) {
  214. pr_err("PM8xxx arb cntrl write failed with %d\n", rc);
  215. return rc;
  216. }
  217. }
  218. if (arb_cntrl) {
  219. data_arb_cntrl |= PM8XXX_ADC_ARB_USRP_CNTRL1_REQ;
  220. INIT_COMPLETION(adc_pmic->adc_rslt_completion);
  221. rc = pm8xxx_writeb(adc_pmic->dev->parent,
  222. PM8XXX_ADC_ARB_USRP_CNTRL1, data_arb_cntrl);
  223. }
  224. return 0;
  225. }
  226. static int32_t pm8xxx_adc_patherm_power(bool on)
  227. {
  228. int rc = 0;
  229. if (!pa_therm) {
  230. pr_err("pm8xxx adc pa_therm not valid\n");
  231. return -EINVAL;
  232. }
  233. if (on) {
  234. rc = regulator_set_voltage(pa_therm,
  235. PM8XXX_ADC_PA_THERM_VREG_UV_MIN,
  236. PM8XXX_ADC_PA_THERM_VREG_UV_MAX);
  237. if (rc < 0) {
  238. pr_err("failed to set the voltage for "
  239. "pa_therm with error %d\n", rc);
  240. return rc;
  241. }
  242. rc = regulator_set_optimum_mode(pa_therm,
  243. PM8XXX_ADC_PA_THERM_VREG_UA_LOAD);
  244. if (rc < 0) {
  245. pr_err("failed to set optimum mode for "
  246. "pa_therm with error %d\n", rc);
  247. return rc;
  248. }
  249. rc = regulator_enable(pa_therm);
  250. if (rc < 0) {
  251. pr_err("failed to enable pa_therm vreg "
  252. "with error %d\n", rc);
  253. return rc;
  254. }
  255. } else {
  256. rc = regulator_disable(pa_therm);
  257. if (rc < 0) {
  258. pr_err("failed to disable pa_therm vreg "
  259. "with error %d\n", rc);
  260. return rc;
  261. }
  262. }
  263. return rc;
  264. }
  265. static int32_t pm8xxx_adc_xo_vote(bool on)
  266. {
  267. struct pm8xxx_adc *adc_pmic = pmic_adc;
  268. if (on)
  269. msm_xo_mode_vote(adc_pmic->adc_voter, MSM_XO_MODE_ON);
  270. else
  271. msm_xo_mode_vote(adc_pmic->adc_voter, MSM_XO_MODE_OFF);
  272. return 0;
  273. }
  274. static int32_t pm8xxx_adc_channel_power_enable(uint32_t channel,
  275. bool power_cntrl)
  276. {
  277. int rc = 0;
  278. switch (channel) {
  279. case ADC_MPP_1_AMUX8:
  280. rc = pm8xxx_adc_patherm_power(power_cntrl);
  281. break;
  282. case CHANNEL_DIE_TEMP:
  283. case CHANNEL_MUXOFF:
  284. rc = pm8xxx_adc_xo_vote(power_cntrl);
  285. break;
  286. default:
  287. break;
  288. }
  289. return rc;
  290. }
  291. static uint32_t pm8xxx_adc_read_reg(uint32_t reg, u8 *data)
  292. {
  293. struct pm8xxx_adc *adc_pmic = pmic_adc;
  294. int rc;
  295. rc = pm8xxx_readb(adc_pmic->dev->parent, reg, data);
  296. if (rc < 0) {
  297. pr_err("PM8xxx adc read reg %d failed with %d\n", reg, rc);
  298. return rc;
  299. }
  300. return 0;
  301. }
  302. static uint32_t pm8xxx_adc_write_reg(uint32_t reg, u8 data)
  303. {
  304. struct pm8xxx_adc *adc_pmic = pmic_adc;
  305. int rc;
  306. rc = pm8xxx_writeb(adc_pmic->dev->parent, reg, data);
  307. if (rc < 0) {
  308. pr_err("PM8xxx adc write reg %d failed with %d\n", reg, rc);
  309. return rc;
  310. }
  311. return 0;
  312. }
  313. static int32_t pm8xxx_adc_configure(
  314. struct pm8xxx_adc_amux_properties *chan_prop)
  315. {
  316. u8 data_amux_chan = 0, data_arb_rsv = 0, data_dig_param = 0;
  317. int rc;
  318. data_amux_chan |= chan_prop->amux_channel << PM8XXX_ADC_AMUX_SEL;
  319. if (chan_prop->amux_mpp_channel)
  320. data_amux_chan |= chan_prop->amux_mpp_channel <<
  321. PM8XXX_ADC_AMUX_MPP_SEL;
  322. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_USRP_AMUX_CNTRL,
  323. data_amux_chan);
  324. if (rc < 0)
  325. return rc;
  326. rc = pm8xxx_adc_read_reg(PM8XXX_ADC_ARB_USRP_RSV, &data_arb_rsv);
  327. if (rc < 0)
  328. return rc;
  329. data_arb_rsv &= (PM8XXX_ADC_ARB_USRP_RSV_RST |
  330. PM8XXX_ADC_ARB_USRP_RSV_DTEST0 |
  331. PM8XXX_ADC_ARB_USRP_RSV_DTEST1 |
  332. PM8XXX_ADC_ARB_USRP_RSV_OP);
  333. data_arb_rsv |= (chan_prop->amux_ip_rsv << PM8XXX_ADC_RSV_IP_SEL |
  334. PM8XXX_ADC_ARB_USRP_RSV_TRM);
  335. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_USRP_RSV, data_arb_rsv);
  336. if (rc < 0)
  337. return rc;
  338. rc = pm8xxx_adc_read_reg(PM8XXX_ADC_ARB_USRP_DIG_PARAM,
  339. &data_dig_param);
  340. if (rc < 0)
  341. return rc;
  342. /* Default 2.4Mhz clock rate */
  343. /* Client chooses the decimation */
  344. switch (chan_prop->decimation) {
  345. case ADC_DECIMATION_TYPE1:
  346. data_dig_param |= PM8XXX_ADC_ARB_USRP_DIG_PARAM_DEC_RATE0;
  347. break;
  348. case ADC_DECIMATION_TYPE2:
  349. data_dig_param |= (PM8XXX_ADC_ARB_USRP_DIG_PARAM_DEC_RATE0
  350. | PM8XXX_ADC_ARB_USRP_DIG_PARAM_DEC_RATE1);
  351. break;
  352. default:
  353. data_dig_param |= PM8XXX_ADC_ARB_USRP_DIG_PARAM_DEC_RATE0;
  354. break;
  355. }
  356. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_USRP_DIG_PARAM,
  357. PM8XXX_ADC_ARB_ANA_DIG);
  358. if (rc < 0)
  359. return rc;
  360. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_USRP_ANA_PARAM,
  361. PM8XXX_ADC_ARB_ANA_DIG);
  362. if (rc < 0)
  363. return rc;
  364. rc = pm8xxx_adc_arb_cntrl(1, data_amux_chan);
  365. if (rc < 0) {
  366. pr_err("Configuring ADC Arbiter"
  367. "enable failed with %d\n", rc);
  368. return rc;
  369. }
  370. return 0;
  371. }
  372. static uint32_t pm8xxx_adc_read_adc_code(int32_t *data)
  373. {
  374. struct pm8xxx_adc *adc_pmic = pmic_adc;
  375. uint8_t rslt_lsb, rslt_msb;
  376. int32_t rc, max_ideal_adc_code = 1 << adc_pmic->adc_prop->bitresolution;
  377. rc = pm8xxx_readb(adc_pmic->dev->parent,
  378. PM8XXX_ADC_ARB_USRP_DATA0, &rslt_lsb);
  379. if (rc < 0) {
  380. pr_err("PM8xxx adc result read failed with %d\n", rc);
  381. return rc;
  382. }
  383. rc = pm8xxx_readb(adc_pmic->dev->parent,
  384. PM8XXX_ADC_ARB_USRP_DATA1, &rslt_msb);
  385. if (rc < 0) {
  386. pr_err("PM8xxx adc result read failed with %d\n", rc);
  387. return rc;
  388. }
  389. *data = (rslt_msb << 8) | rslt_lsb;
  390. /* Use the midpoint to determine underflow or overflow */
  391. if (*data > max_ideal_adc_code + (max_ideal_adc_code >> 1))
  392. *data |= ((1 << (8 * sizeof(*data) -
  393. adc_pmic->adc_prop->bitresolution)) - 1) <<
  394. adc_pmic->adc_prop->bitresolution;
  395. /* Default value for switching off the arbiter after reading
  396. the ADC value. Bit 0 set to 0. */
  397. rc = pm8xxx_adc_arb_cntrl(0, CHANNEL_NONE);
  398. if (rc < 0) {
  399. pr_err("%s: Configuring ADC Arbiter disable"
  400. "failed\n", __func__);
  401. return rc;
  402. }
  403. return 0;
  404. }
  405. static void pm8xxx_adc_btm_warm_scheduler_fn(struct work_struct *work)
  406. {
  407. struct pm8xxx_adc *adc_pmic = container_of(work, struct pm8xxx_adc,
  408. warm_work);
  409. unsigned long flags = 0;
  410. bool warm_status;
  411. spin_lock_irqsave(&adc_pmic->btm_lock, flags);
  412. warm_status = irq_read_line(adc_pmic->btm_warm_irq);
  413. if (adc_pmic->batt.btm_warm_fn != NULL)
  414. adc_pmic->batt.btm_warm_fn(warm_status);
  415. spin_unlock_irqrestore(&adc_pmic->btm_lock, flags);
  416. }
  417. static void pm8xxx_adc_btm_cool_scheduler_fn(struct work_struct *work)
  418. {
  419. struct pm8xxx_adc *adc_pmic = container_of(work, struct pm8xxx_adc,
  420. cool_work);
  421. unsigned long flags = 0;
  422. bool cool_status;
  423. spin_lock_irqsave(&adc_pmic->btm_lock, flags);
  424. cool_status = irq_read_line(adc_pmic->btm_cool_irq);
  425. if (adc_pmic->batt.btm_cool_fn != NULL)
  426. adc_pmic->batt.btm_cool_fn(cool_status);
  427. spin_unlock_irqrestore(&adc_pmic->btm_lock, flags);
  428. }
  429. void trigger_completion(struct work_struct *work)
  430. {
  431. struct pm8xxx_adc *adc_8xxx = pmic_adc;
  432. complete(&adc_8xxx->adc_rslt_completion);
  433. }
  434. DECLARE_WORK(trigger_completion_work, trigger_completion);
  435. static irqreturn_t pm8xxx_adc_isr(int irq, void *dev_id)
  436. {
  437. if (pm8xxx_adc_calib_first_adc)
  438. return IRQ_HANDLED;
  439. schedule_work(&trigger_completion_work);
  440. return IRQ_HANDLED;
  441. }
  442. static irqreturn_t pm8xxx_btm_warm_isr(int irq, void *dev_id)
  443. {
  444. struct pm8xxx_adc *btm_8xxx = dev_id;
  445. schedule_work(&btm_8xxx->warm_work);
  446. return IRQ_HANDLED;
  447. }
  448. static irqreturn_t pm8xxx_btm_cool_isr(int irq, void *dev_id)
  449. {
  450. struct pm8xxx_adc *btm_8xxx = dev_id;
  451. schedule_work(&btm_8xxx->cool_work);
  452. return IRQ_HANDLED;
  453. }
  454. static uint32_t pm8xxx_adc_calib_device(void)
  455. {
  456. struct pm8xxx_adc *adc_pmic = pmic_adc;
  457. struct pm8xxx_adc_amux_properties conv;
  458. int rc, calib_read_1, calib_read_2;
  459. u8 data_arb_usrp_cntrl1 = 0;
  460. conv.amux_channel = CHANNEL_125V;
  461. conv.decimation = ADC_DECIMATION_TYPE2;
  462. conv.amux_ip_rsv = AMUX_RSV1;
  463. conv.amux_mpp_channel = PREMUX_MPP_SCALE_0;
  464. pm8xxx_adc_calib_first_adc = true;
  465. rc = pm8xxx_adc_configure(&conv);
  466. if (rc) {
  467. pr_err("pm8xxx_adc configure failed with %d\n", rc);
  468. goto calib_fail;
  469. }
  470. while (data_arb_usrp_cntrl1 != (PM8XXX_ADC_ARB_USRP_CNTRL1_EOC |
  471. PM8XXX_ADC_ARB_USRP_CNTRL1_EN_ARB)) {
  472. rc = pm8xxx_adc_read_reg(PM8XXX_ADC_ARB_USRP_CNTRL1,
  473. &data_arb_usrp_cntrl1);
  474. if (rc < 0)
  475. return rc;
  476. usleep_range(PM8XXX_ADC_CONV_TIME_MIN,
  477. PM8XXX_ADC_CONV_TIME_MAX);
  478. }
  479. data_arb_usrp_cntrl1 = 0;
  480. rc = pm8xxx_adc_read_adc_code(&calib_read_1);
  481. if (rc) {
  482. pr_err("pm8xxx_adc read adc failed with %d\n", rc);
  483. pm8xxx_adc_calib_first_adc = false;
  484. goto calib_fail;
  485. }
  486. pm8xxx_adc_calib_first_adc = false;
  487. conv.amux_channel = CHANNEL_625MV;
  488. conv.decimation = ADC_DECIMATION_TYPE2;
  489. conv.amux_ip_rsv = AMUX_RSV1;
  490. conv.amux_mpp_channel = PREMUX_MPP_SCALE_0;
  491. pm8xxx_adc_calib_first_adc = true;
  492. rc = pm8xxx_adc_configure(&conv);
  493. if (rc) {
  494. pr_err("pm8xxx_adc configure failed with %d\n", rc);
  495. goto calib_fail;
  496. }
  497. while (data_arb_usrp_cntrl1 != (PM8XXX_ADC_ARB_USRP_CNTRL1_EOC |
  498. PM8XXX_ADC_ARB_USRP_CNTRL1_EN_ARB)) {
  499. rc = pm8xxx_adc_read_reg(PM8XXX_ADC_ARB_USRP_CNTRL1,
  500. &data_arb_usrp_cntrl1);
  501. if (rc < 0)
  502. return rc;
  503. usleep_range(PM8XXX_ADC_CONV_TIME_MIN,
  504. PM8XXX_ADC_CONV_TIME_MAX);
  505. }
  506. data_arb_usrp_cntrl1 = 0;
  507. rc = pm8xxx_adc_read_adc_code(&calib_read_2);
  508. if (rc) {
  509. pr_err("pm8xxx_adc read adc failed with %d\n", rc);
  510. pm8xxx_adc_calib_first_adc = false;
  511. goto calib_fail;
  512. }
  513. pm8xxx_adc_calib_first_adc = false;
  514. adc_pmic->conv->chan_prop->adc_graph[ADC_CALIB_ABSOLUTE].dy =
  515. (calib_read_1 - calib_read_2);
  516. adc_pmic->conv->chan_prop->adc_graph[ADC_CALIB_ABSOLUTE].dx
  517. = PM8XXX_CHANNEL_ADC_625_UV;
  518. adc_pmic->conv->chan_prop->adc_graph[ADC_CALIB_ABSOLUTE].adc_vref =
  519. calib_read_1;
  520. adc_pmic->conv->chan_prop->adc_graph[ADC_CALIB_ABSOLUTE].adc_gnd =
  521. calib_read_2;
  522. rc = pm8xxx_adc_arb_cntrl(0, CHANNEL_NONE);
  523. if (rc < 0) {
  524. pr_err("%s: Configuring ADC Arbiter disable"
  525. "failed\n", __func__);
  526. return rc;
  527. }
  528. /* Ratiometric Calibration */
  529. conv.amux_channel = CHANNEL_MUXOFF;
  530. conv.decimation = ADC_DECIMATION_TYPE2;
  531. conv.amux_ip_rsv = AMUX_RSV5;
  532. conv.amux_mpp_channel = PREMUX_MPP_SCALE_0;
  533. pm8xxx_adc_calib_first_adc = true;
  534. rc = pm8xxx_adc_configure(&conv);
  535. if (rc) {
  536. pr_err("pm8xxx_adc configure failed with %d\n", rc);
  537. goto calib_fail;
  538. }
  539. while (data_arb_usrp_cntrl1 != (PM8XXX_ADC_ARB_USRP_CNTRL1_EOC |
  540. PM8XXX_ADC_ARB_USRP_CNTRL1_EN_ARB)) {
  541. rc = pm8xxx_adc_read_reg(PM8XXX_ADC_ARB_USRP_CNTRL1,
  542. &data_arb_usrp_cntrl1);
  543. if (rc < 0)
  544. return rc;
  545. usleep_range(PM8XXX_ADC_CONV_TIME_MIN,
  546. PM8XXX_ADC_CONV_TIME_MAX);
  547. }
  548. data_arb_usrp_cntrl1 = 0;
  549. rc = pm8xxx_adc_read_adc_code(&calib_read_1);
  550. if (rc) {
  551. pr_err("pm8xxx_adc read adc failed with %d\n", rc);
  552. pm8xxx_adc_calib_first_adc = false;
  553. goto calib_fail;
  554. }
  555. pm8xxx_adc_calib_first_adc = false;
  556. conv.amux_channel = CHANNEL_MUXOFF;
  557. conv.decimation = ADC_DECIMATION_TYPE2;
  558. conv.amux_ip_rsv = AMUX_RSV4;
  559. conv.amux_mpp_channel = PREMUX_MPP_SCALE_0;
  560. pm8xxx_adc_calib_first_adc = true;
  561. rc = pm8xxx_adc_configure(&conv);
  562. if (rc) {
  563. pr_err("pm8xxx_adc configure failed with %d\n", rc);
  564. goto calib_fail;
  565. }
  566. while (data_arb_usrp_cntrl1 != (PM8XXX_ADC_ARB_USRP_CNTRL1_EOC |
  567. PM8XXX_ADC_ARB_USRP_CNTRL1_EN_ARB)) {
  568. rc = pm8xxx_adc_read_reg(PM8XXX_ADC_ARB_USRP_CNTRL1,
  569. &data_arb_usrp_cntrl1);
  570. if (rc < 0)
  571. return rc;
  572. usleep_range(PM8XXX_ADC_CONV_TIME_MIN,
  573. PM8XXX_ADC_CONV_TIME_MAX);
  574. }
  575. data_arb_usrp_cntrl1 = 0;
  576. rc = pm8xxx_adc_read_adc_code(&calib_read_2);
  577. if (rc) {
  578. pr_err("pm8xxx_adc read adc failed with %d\n", rc);
  579. pm8xxx_adc_calib_first_adc = false;
  580. goto calib_fail;
  581. }
  582. pm8xxx_adc_calib_first_adc = false;
  583. adc_pmic->conv->chan_prop->adc_graph[ADC_CALIB_RATIOMETRIC].dy =
  584. (calib_read_1 - calib_read_2);
  585. adc_pmic->conv->chan_prop->adc_graph[ADC_CALIB_RATIOMETRIC].dx =
  586. adc_pmic->adc_prop->adc_vdd_reference;
  587. adc_pmic->conv->chan_prop->adc_graph[ADC_CALIB_RATIOMETRIC].adc_vref =
  588. calib_read_1;
  589. adc_pmic->conv->chan_prop->adc_graph[ADC_CALIB_RATIOMETRIC].adc_gnd =
  590. calib_read_2;
  591. calib_fail:
  592. rc = pm8xxx_adc_arb_cntrl(0, CHANNEL_NONE);
  593. if (rc < 0) {
  594. pr_err("%s: Configuring ADC Arbiter disable"
  595. "failed\n", __func__);
  596. }
  597. return rc;
  598. }
  599. uint32_t pm8xxx_adc_read(enum pm8xxx_adc_channels channel,
  600. struct pm8xxx_adc_chan_result *result)
  601. {
  602. struct pm8xxx_adc *adc_pmic = pmic_adc;
  603. int i = 0, rc = 0, rc_fail, amux_prescaling, scale_type;
  604. enum pm8xxx_adc_premux_mpp_scale_type mpp_scale;
  605. if (!pm8xxx_adc_initialized)
  606. return -ENODEV;
  607. if (!pm8xxx_adc_calib_device_init) {
  608. if (pm8xxx_adc_calib_device() == 0)
  609. pm8xxx_adc_calib_device_init = true;
  610. }
  611. mutex_lock(&adc_pmic->adc_lock);
  612. for (i = 0; i < adc_pmic->adc_num_board_channel; i++) {
  613. if (channel == adc_pmic->adc_channel[i].channel_name)
  614. break;
  615. }
  616. if (i == adc_pmic->adc_num_board_channel ||
  617. (pm8xxx_adc_check_channel_valid(channel) != 0)) {
  618. rc = -EBADF;
  619. goto fail_unlock;
  620. }
  621. if (channel < PM8XXX_CHANNEL_MPP_SCALE1_IDX) {
  622. mpp_scale = PREMUX_MPP_SCALE_0;
  623. adc_pmic->conv->amux_channel = channel;
  624. } else if (channel >= PM8XXX_CHANNEL_MPP_SCALE1_IDX &&
  625. channel < PM8XXX_CHANNEL_MPP_SCALE3_IDX) {
  626. mpp_scale = PREMUX_MPP_SCALE_1;
  627. adc_pmic->conv->amux_channel = channel %
  628. PM8XXX_CHANNEL_MPP_SCALE1_IDX;
  629. } else {
  630. mpp_scale = PREMUX_MPP_SCALE_1_DIV3;
  631. adc_pmic->conv->amux_channel = channel %
  632. PM8XXX_CHANNEL_MPP_SCALE3_IDX;
  633. }
  634. adc_pmic->conv->amux_mpp_channel = mpp_scale;
  635. adc_pmic->conv->amux_ip_rsv = adc_pmic->adc_channel[i].adc_rsv;
  636. adc_pmic->conv->decimation = adc_pmic->adc_channel[i].adc_decimation;
  637. amux_prescaling = adc_pmic->adc_channel[i].chan_path_prescaling;
  638. adc_pmic->conv->chan_prop->offset_gain_numerator =
  639. pm8xxx_amux_scaling_ratio[amux_prescaling].num;
  640. adc_pmic->conv->chan_prop->offset_gain_denominator =
  641. pm8xxx_amux_scaling_ratio[amux_prescaling].den;
  642. rc = pm8xxx_adc_channel_power_enable(channel, true);
  643. if (rc) {
  644. rc = -EINVAL;
  645. goto fail_unlock;
  646. }
  647. rc = pm8xxx_adc_configure(adc_pmic->conv);
  648. if (rc) {
  649. rc = -EINVAL;
  650. goto fail;
  651. }
  652. rc = wait_for_completion_timeout(&adc_pmic->adc_rslt_completion,
  653. PM8XXX_ADC_COMPLETION_TIMEOUT);
  654. if (!rc) {
  655. u8 data_arb_usrp_cntrl1 = 0;
  656. rc = pm8xxx_adc_read_reg(PM8XXX_ADC_ARB_USRP_CNTRL1,
  657. &data_arb_usrp_cntrl1);
  658. if (rc < 0)
  659. goto fail;
  660. if (data_arb_usrp_cntrl1 == (PM8XXX_ADC_ARB_USRP_CNTRL1_EOC |
  661. PM8XXX_ADC_ARB_USRP_CNTRL1_EN_ARB))
  662. pr_debug("End of conversion status set\n");
  663. else {
  664. pr_err("EOC interrupt not received\n");
  665. rc = -EINVAL;
  666. goto fail;
  667. }
  668. }
  669. rc = pm8xxx_adc_read_adc_code(&result->adc_code);
  670. if (rc) {
  671. rc = -EINVAL;
  672. goto fail;
  673. }
  674. scale_type = adc_pmic->adc_channel[i].adc_scale_fn;
  675. if (scale_type >= ADC_SCALE_NONE) {
  676. rc = -EBADF;
  677. goto fail;
  678. }
  679. adc_scale_fn[scale_type].chan(result->adc_code,
  680. adc_pmic->adc_prop, adc_pmic->conv->chan_prop, result);
  681. rc = pm8xxx_adc_channel_power_enable(channel, false);
  682. if (rc) {
  683. rc = -EINVAL;
  684. goto fail_unlock;
  685. }
  686. mutex_unlock(&adc_pmic->adc_lock);
  687. return 0;
  688. fail:
  689. rc_fail = pm8xxx_adc_channel_power_enable(channel, false);
  690. if (rc_fail)
  691. pr_err("pm8xxx adc power disable failed\n");
  692. fail_unlock:
  693. mutex_unlock(&adc_pmic->adc_lock);
  694. pr_err("pm8xxx adc error with %d\n", rc);
  695. return rc;
  696. }
  697. EXPORT_SYMBOL_GPL(pm8xxx_adc_read);
  698. uint32_t pm8xxx_adc_mpp_config_read(uint32_t mpp_num,
  699. enum pm8xxx_adc_channels channel,
  700. struct pm8xxx_adc_chan_result *result)
  701. {
  702. struct pm8xxx_adc *adc_pmic = pmic_adc;
  703. int rc = 0;
  704. if (!pm8xxx_adc_initialized)
  705. return -ENODEV;
  706. if (!adc_pmic->mpp_base) {
  707. rc = -EINVAL;
  708. pr_info("PM8xxx MPP base invalid with error %d\n", rc);
  709. return rc;
  710. }
  711. if (mpp_num == PM8XXX_AMUX_MPP_8) {
  712. rc = -EINVAL;
  713. pr_info("PM8xxx MPP8 is already configured "
  714. "to AMUX8. Use pm8xxx_adc_read() instead.\n");
  715. return rc;
  716. }
  717. mutex_lock(&adc_pmic->mpp_adc_lock);
  718. rc = pm8xxx_mpp_config(((mpp_num - 1) + adc_pmic->mpp_base),
  719. &pm8xxx_adc_mpp_config);
  720. if (rc < 0) {
  721. pr_err("pm8xxx adc mpp config error with %d\n", rc);
  722. goto fail;
  723. }
  724. usleep_range(PM8XXX_ADC_MPP_SETTLE_TIME_MIN,
  725. PM8XXX_ADC_MPP_SETTLE_TIME_MAX);
  726. rc = pm8xxx_adc_read(channel, result);
  727. if (rc < 0)
  728. pr_err("pm8xxx adc read error with %d\n", rc);
  729. rc = pm8xxx_mpp_config(((mpp_num - 1) + adc_pmic->mpp_base),
  730. &pm8xxx_adc_mpp_unconfig);
  731. if (rc < 0)
  732. pr_err("pm8xxx adc mpp config error with %d\n", rc);
  733. fail:
  734. mutex_unlock(&adc_pmic->mpp_adc_lock);
  735. return rc;
  736. }
  737. EXPORT_SYMBOL_GPL(pm8xxx_adc_mpp_config_read);
  738. uint32_t pm8xxx_adc_btm_configure(struct pm8xxx_adc_arb_btm_param *btm_param)
  739. {
  740. struct pm8xxx_adc *adc_pmic = pmic_adc;
  741. u8 data_btm_cool_thr0, data_btm_cool_thr1;
  742. u8 data_btm_warm_thr0, data_btm_warm_thr1;
  743. u8 arb_btm_cntrl1;
  744. unsigned long flags = 0;
  745. int rc;
  746. if (adc_pmic == NULL) {
  747. pr_err("PMIC ADC not valid\n");
  748. return -EINVAL;
  749. }
  750. if ((btm_param->btm_cool_fn == NULL) &&
  751. (btm_param->btm_warm_fn == NULL)) {
  752. pr_err("No BTM warm/cool notification??\n");
  753. return -EINVAL;
  754. }
  755. rc = pm8xxx_adc_batt_scaler(btm_param, adc_pmic->adc_prop,
  756. adc_pmic->conv->chan_prop);
  757. if (rc < 0) {
  758. pr_err("Failed to lookup the BTM thresholds\n");
  759. return rc;
  760. }
  761. if (btm_param->interval > PM8XXX_ADC_BTM_INTERVAL_MAX) {
  762. pr_info("Bug in PMIC BTM interval time and cannot set"
  763. " a value greater than 0x14 %x\n", btm_param->interval);
  764. btm_param->interval = PM8XXX_ADC_BTM_INTERVAL_MAX;
  765. }
  766. spin_lock_irqsave(&adc_pmic->btm_lock, flags);
  767. data_btm_cool_thr0 = ((btm_param->low_thr_voltage << 24) >> 24);
  768. data_btm_cool_thr1 = ((btm_param->low_thr_voltage << 16) >> 24);
  769. data_btm_warm_thr0 = ((btm_param->high_thr_voltage << 24) >> 24);
  770. data_btm_warm_thr1 = ((btm_param->high_thr_voltage << 16) >> 24);
  771. if (btm_param->btm_cool_fn != NULL) {
  772. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_BAT_COOL_THR0,
  773. data_btm_cool_thr0);
  774. if (rc < 0)
  775. goto write_err;
  776. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_BAT_COOL_THR1,
  777. data_btm_cool_thr1);
  778. if (rc < 0)
  779. goto write_err;
  780. adc_pmic->batt.btm_cool_fn = btm_param->btm_cool_fn;
  781. }
  782. if (btm_param->btm_warm_fn != NULL) {
  783. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_BAT_WARM_THR0,
  784. data_btm_warm_thr0);
  785. if (rc < 0)
  786. goto write_err;
  787. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_BAT_WARM_THR1,
  788. data_btm_warm_thr1);
  789. if (rc < 0)
  790. goto write_err;
  791. adc_pmic->batt.btm_warm_fn = btm_param->btm_warm_fn;
  792. }
  793. rc = pm8xxx_adc_read_reg(PM8XXX_ADC_ARB_BTM_CNTRL1, &arb_btm_cntrl1);
  794. if (rc < 0)
  795. goto bail_out;
  796. btm_param->interval &= PM8XXX_ADC_BTM_INTERVAL_SEL_MASK;
  797. arb_btm_cntrl1 |=
  798. btm_param->interval << PM8XXX_ADC_BTM_INTERVAL_SEL_SHIFT;
  799. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_CNTRL1, arb_btm_cntrl1);
  800. if (rc < 0)
  801. goto write_err;
  802. spin_unlock_irqrestore(&adc_pmic->btm_lock, flags);
  803. return rc;
  804. bail_out:
  805. write_err:
  806. spin_unlock_irqrestore(&adc_pmic->btm_lock, flags);
  807. pr_debug("%s: with error code %d\n", __func__, rc);
  808. return rc;
  809. }
  810. EXPORT_SYMBOL_GPL(pm8xxx_adc_btm_configure);
  811. static uint32_t pm8xxx_adc_btm_read(uint32_t channel)
  812. {
  813. struct pm8xxx_adc *adc_pmic = pmic_adc;
  814. int rc, i;
  815. u8 arb_btm_dig_param, arb_btm_ana_param, arb_btm_rsv;
  816. u8 arb_btm_amux_cntrl, data_arb_btm_cntrl = 0;
  817. unsigned long flags;
  818. arb_btm_amux_cntrl = channel << PM8XXX_ADC_BTM_CHANNEL_SEL;
  819. arb_btm_rsv = adc_pmic->adc_channel[channel].adc_rsv;
  820. arb_btm_dig_param = arb_btm_ana_param = PM8XXX_ADC_ARB_ANA_DIG;
  821. spin_lock_irqsave(&adc_pmic->btm_lock, flags);
  822. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_AMUX_CNTRL,
  823. arb_btm_amux_cntrl);
  824. if (rc < 0)
  825. goto write_err;
  826. arb_btm_rsv = PM8XXX_ADC_BTM_RSV;
  827. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_RSV, arb_btm_rsv);
  828. if (rc < 0)
  829. goto write_err;
  830. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_DIG_PARAM,
  831. arb_btm_dig_param);
  832. if (rc < 0)
  833. goto write_err;
  834. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_ANA_PARAM,
  835. arb_btm_ana_param);
  836. if (rc < 0)
  837. goto write_err;
  838. data_arb_btm_cntrl |= PM8XXX_ADC_ARB_BTM_CNTRL1_EN_BTM;
  839. for (i = 0; i < 2; i++) {
  840. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_CNTRL1,
  841. data_arb_btm_cntrl);
  842. if (rc < 0)
  843. goto write_err;
  844. }
  845. data_arb_btm_cntrl |= PM8XXX_ADC_ARB_BTM_CNTRL1_REQ
  846. | PM8XXX_ADC_ARB_BTM_CNTRL1_SEL_OP_MODE;
  847. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_CNTRL1,
  848. data_arb_btm_cntrl);
  849. if (rc < 0)
  850. goto write_err;
  851. if (pmic_adc->batt.btm_warm_fn != NULL)
  852. enable_irq(adc_pmic->btm_warm_irq);
  853. if (pmic_adc->batt.btm_cool_fn != NULL)
  854. enable_irq(adc_pmic->btm_cool_irq);
  855. write_err:
  856. spin_unlock_irqrestore(&adc_pmic->btm_lock, flags);
  857. return rc;
  858. }
  859. uint32_t pm8xxx_adc_btm_start(void)
  860. {
  861. return pm8xxx_adc_btm_read(CHANNEL_BATT_THERM);
  862. }
  863. EXPORT_SYMBOL_GPL(pm8xxx_adc_btm_start);
  864. uint32_t pm8xxx_adc_btm_end(void)
  865. {
  866. struct pm8xxx_adc *adc_pmic = pmic_adc;
  867. int i, rc;
  868. u8 data_arb_btm_cntrl = 0;
  869. unsigned long flags;
  870. disable_irq_nosync(adc_pmic->btm_warm_irq);
  871. disable_irq_nosync(adc_pmic->btm_cool_irq);
  872. spin_lock_irqsave(&adc_pmic->btm_lock, flags);
  873. /* Write twice to the CNTRL register for the arbiter settings
  874. to take into effect */
  875. for (i = 0; i < 2; i++) {
  876. rc = pm8xxx_adc_write_reg(PM8XXX_ADC_ARB_BTM_CNTRL1,
  877. data_arb_btm_cntrl);
  878. if (rc < 0) {
  879. spin_unlock_irqrestore(&adc_pmic->btm_lock, flags);
  880. return rc;
  881. }
  882. }
  883. spin_unlock_irqrestore(&adc_pmic->btm_lock, flags);
  884. return rc;
  885. }
  886. EXPORT_SYMBOL_GPL(pm8xxx_adc_btm_end);
  887. static ssize_t pm8xxx_adc_show(struct device *dev,
  888. struct device_attribute *devattr, char *buf)
  889. {
  890. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  891. struct pm8xxx_adc_chan_result result;
  892. int rc = -1;
  893. rc = pm8xxx_adc_read(attr->index, &result);
  894. if (rc)
  895. return 0;
  896. return snprintf(buf, PM8XXX_ADC_HWMON_NAME_LENGTH,
  897. "Result:%lld Raw:%d\n", result.physical, result.adc_code);
  898. }
  899. static int get_adc(void *data, u64 *val)
  900. {
  901. struct pm8xxx_adc_chan_result result;
  902. int i = (int)data;
  903. int rc;
  904. rc = pm8xxx_adc_read(i, &result);
  905. if (!rc)
  906. pr_info("ADC value raw:%x physical:%lld\n",
  907. result.adc_code, result.physical);
  908. *val = result.physical;
  909. return 0;
  910. }
  911. DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_adc, NULL, "%lld\n");
  912. #ifdef CONFIG_DEBUG_FS
  913. static void create_debugfs_entries(void)
  914. {
  915. int i = 0;
  916. pmic_adc->dent = debugfs_create_dir("pm8xxx_adc", NULL);
  917. if (IS_ERR(pmic_adc->dent)) {
  918. pr_err("pmic adc debugfs dir not created\n");
  919. return;
  920. }
  921. for (i = 0; i < pmic_adc->adc_num_board_channel; i++)
  922. debugfs_create_file(pmic_adc->adc_channel[i].name,
  923. 0644, pmic_adc->dent,
  924. (void *)pmic_adc->adc_channel[i].channel_name,
  925. &reg_fops);
  926. }
  927. #else
  928. static inline void create_debugfs_entries(void)
  929. {
  930. }
  931. #endif
  932. static struct sensor_device_attribute pm8xxx_adc_attr =
  933. SENSOR_ATTR(NULL, S_IRUGO, pm8xxx_adc_show, NULL, 0);
  934. static int32_t pm8xxx_adc_init_hwmon(struct platform_device *pdev)
  935. {
  936. struct pm8xxx_adc *adc_pmic = pmic_adc;
  937. int rc = 0, i, channel;
  938. for (i = 0; i < pmic_adc->adc_num_board_channel; i++) {
  939. channel = adc_pmic->adc_channel[i].channel_name;
  940. if (pm8xxx_adc_check_channel_valid(channel)) {
  941. pr_err("Invalid ADC init HWMON channel: %d\n", channel);
  942. continue;
  943. }
  944. pm8xxx_adc_attr.index = adc_pmic->adc_channel[i].channel_name;
  945. pm8xxx_adc_attr.dev_attr.attr.name =
  946. adc_pmic->adc_channel[i].name;
  947. memcpy(&adc_pmic->sens_attr[i], &pm8xxx_adc_attr,
  948. sizeof(pm8xxx_adc_attr));
  949. sysfs_attr_init(&adc_pmic->sens_attr[i].dev_attr.attr);
  950. rc = device_create_file(&pdev->dev,
  951. &adc_pmic->sens_attr[i].dev_attr);
  952. if (rc) {
  953. dev_err(&pdev->dev, "device_create_file failed for "
  954. "dev %s\n",
  955. adc_pmic->adc_channel[i].name);
  956. goto hwmon_err_sens;
  957. }
  958. }
  959. return 0;
  960. hwmon_err_sens:
  961. pr_info("Init HWMON failed for pm8xxx_adc with %d\n", rc);
  962. return rc;
  963. }
  964. #ifdef CONFIG_PM
  965. static int pm8xxx_adc_suspend_noirq(struct device *dev)
  966. {
  967. struct pm8xxx_adc *adc_pmic = pmic_adc;
  968. adc_pmic->msm_suspend_check = 1;
  969. return 0;
  970. }
  971. static int pm8xxx_adc_resume_noirq(struct device *dev)
  972. {
  973. struct pm8xxx_adc *adc_pmic = pmic_adc;
  974. adc_pmic->msm_suspend_check = 0;
  975. return 0;
  976. }
  977. static const struct dev_pm_ops pm8xxx_adc_dev_pm_ops = {
  978. .suspend_noirq = pm8xxx_adc_suspend_noirq,
  979. .resume_noirq = pm8xxx_adc_resume_noirq,
  980. };
  981. #define PM8XXX_ADC_DEV_PM_OPS (&pm8xxx_adc_dev_pm_ops)
  982. #else
  983. #define PM8XXX_ADC_DEV_PM_OPS NULL
  984. #endif
  985. static int __devexit pm8xxx_adc_teardown(struct platform_device *pdev)
  986. {
  987. struct pm8xxx_adc *adc_pmic = pmic_adc;
  988. int i;
  989. msm_xo_put(adc_pmic->adc_voter);
  990. platform_set_drvdata(pdev, NULL);
  991. pmic_adc = NULL;
  992. if (!pa_therm) {
  993. regulator_put(pa_therm);
  994. pa_therm = NULL;
  995. }
  996. for (i = 0; i < adc_pmic->adc_num_board_channel; i++)
  997. device_remove_file(adc_pmic->dev,
  998. &adc_pmic->sens_attr[i].dev_attr);
  999. pm8xxx_adc_initialized = false;
  1000. return 0;
  1001. }
  1002. static int __devinit pm8xxx_adc_probe(struct platform_device *pdev)
  1003. {
  1004. const struct pm8xxx_adc_platform_data *pdata = pdev->dev.platform_data;
  1005. struct pm8xxx_adc *adc_pmic;
  1006. struct pm8xxx_adc_amux_properties *adc_amux_prop;
  1007. int rc = 0;
  1008. if (!pdata) {
  1009. dev_err(&pdev->dev, "no platform data?\n");
  1010. return -EINVAL;
  1011. }
  1012. adc_pmic = devm_kzalloc(&pdev->dev, sizeof(struct pm8xxx_adc) +
  1013. (sizeof(struct sensor_device_attribute) *
  1014. pdata->adc_num_board_channel), GFP_KERNEL);
  1015. if (!adc_pmic) {
  1016. dev_err(&pdev->dev, "Unable to allocate memory\n");
  1017. return -ENOMEM;
  1018. }
  1019. adc_amux_prop = devm_kzalloc(&pdev->dev,
  1020. sizeof(struct pm8xxx_adc_amux_properties) +
  1021. sizeof(struct pm8xxx_adc_chan_properties)
  1022. , GFP_KERNEL);
  1023. if (!adc_amux_prop) {
  1024. dev_err(&pdev->dev, "Unable to allocate memory\n");
  1025. return -ENOMEM;
  1026. }
  1027. adc_pmic->dev = &pdev->dev;
  1028. adc_pmic->adc_prop = pdata->adc_prop;
  1029. adc_pmic->conv = adc_amux_prop;
  1030. init_completion(&adc_pmic->adc_rslt_completion);
  1031. adc_pmic->adc_channel = pdata->adc_channel;
  1032. adc_pmic->adc_num_board_channel = pdata->adc_num_board_channel;
  1033. adc_pmic->mpp_base = pdata->adc_mpp_base;
  1034. mutex_init(&adc_pmic->adc_lock);
  1035. mutex_init(&adc_pmic->mpp_adc_lock);
  1036. spin_lock_init(&adc_pmic->btm_lock);
  1037. adc_pmic->adc_irq = platform_get_irq(pdev, PM8XXX_ADC_IRQ_0);
  1038. if (adc_pmic->adc_irq < 0)
  1039. return adc_pmic->adc_irq;
  1040. rc = devm_request_irq(&pdev->dev, adc_pmic->adc_irq,
  1041. pm8xxx_adc_isr,
  1042. IRQF_TRIGGER_RISING, "pm8xxx_adc_interrupt", adc_pmic);
  1043. if (rc) {
  1044. dev_err(&pdev->dev, "failed to request adc irq "
  1045. "with error %d\n", rc);
  1046. } else {
  1047. enable_irq_wake(adc_pmic->adc_irq);
  1048. }
  1049. adc_pmic->btm_warm_irq = platform_get_irq(pdev, PM8XXX_ADC_IRQ_1);
  1050. if (adc_pmic->btm_warm_irq < 0)
  1051. return adc_pmic->btm_warm_irq;
  1052. rc = devm_request_irq(&pdev->dev, adc_pmic->btm_warm_irq,
  1053. pm8xxx_btm_warm_isr,
  1054. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  1055. "pm8xxx_btm_warm_interrupt", adc_pmic);
  1056. if (rc) {
  1057. pr_err("btm warm irq failed %d with interrupt number %d\n",
  1058. rc, adc_pmic->btm_warm_irq);
  1059. dev_err(&pdev->dev, "failed to request btm irq\n");
  1060. }
  1061. disable_irq_nosync(adc_pmic->btm_warm_irq);
  1062. adc_pmic->btm_cool_irq = platform_get_irq(pdev, PM8XXX_ADC_IRQ_2);
  1063. if (adc_pmic->btm_cool_irq < 0)
  1064. return adc_pmic->btm_cool_irq;
  1065. rc = devm_request_irq(&pdev->dev, adc_pmic->btm_cool_irq,
  1066. pm8xxx_btm_cool_isr,
  1067. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  1068. "pm8xxx_btm_cool_interrupt", adc_pmic);
  1069. if (rc) {
  1070. pr_err("btm cool irq failed with return %d and number %d\n",
  1071. rc, adc_pmic->btm_cool_irq);
  1072. dev_err(&pdev->dev, "failed to request btm irq\n");
  1073. }
  1074. disable_irq_nosync(adc_pmic->btm_cool_irq);
  1075. platform_set_drvdata(pdev, adc_pmic);
  1076. adc_pmic->msm_suspend_check = 0;
  1077. pmic_adc = adc_pmic;
  1078. INIT_WORK(&adc_pmic->warm_work, pm8xxx_adc_btm_warm_scheduler_fn);
  1079. INIT_WORK(&adc_pmic->cool_work, pm8xxx_adc_btm_cool_scheduler_fn);
  1080. create_debugfs_entries();
  1081. pm8xxx_adc_calib_first_adc = false;
  1082. pm8xxx_adc_calib_device_init = false;
  1083. pm8xxx_adc_initialized = true;
  1084. rc = pm8xxx_adc_init_hwmon(pdev);
  1085. if (rc) {
  1086. pr_err("pm8xxx adc init hwmon failed with %d\n", rc);
  1087. dev_err(&pdev->dev, "failed to initialize pm8xxx hwmon adc\n");
  1088. }
  1089. adc_pmic->hwmon = hwmon_device_register(adc_pmic->dev);
  1090. if (adc_pmic->adc_voter == NULL) {
  1091. adc_pmic->adc_voter = msm_xo_get(MSM_XO_TCXO_D0, "pmic_xoadc");
  1092. if (IS_ERR(adc_pmic->adc_voter)) {
  1093. dev_err(&pdev->dev, "Failed to get XO vote\n");
  1094. return PTR_ERR(adc_pmic->adc_voter);
  1095. }
  1096. }
  1097. pa_therm = regulator_get(adc_pmic->dev, "pa_therm");
  1098. if (IS_ERR(pa_therm)) {
  1099. rc = PTR_ERR(pa_therm);
  1100. pr_err("failed to request pa_therm vreg with error %d\n", rc);
  1101. pa_therm = NULL;
  1102. }
  1103. return 0;
  1104. }
  1105. static struct platform_driver pm8xxx_adc_driver = {
  1106. .probe = pm8xxx_adc_probe,
  1107. .remove = __devexit_p(pm8xxx_adc_teardown),
  1108. .driver = {
  1109. .name = PM8XXX_ADC_DEV_NAME,
  1110. .owner = THIS_MODULE,
  1111. .pm = PM8XXX_ADC_DEV_PM_OPS,
  1112. },
  1113. };
  1114. static int __init pm8xxx_adc_init(void)
  1115. {
  1116. return platform_driver_register(&pm8xxx_adc_driver);
  1117. }
  1118. module_init(pm8xxx_adc_init);
  1119. static void __exit pm8xxx_adc_exit(void)
  1120. {
  1121. platform_driver_unregister(&pm8xxx_adc_driver);
  1122. }
  1123. module_exit(pm8xxx_adc_exit);
  1124. MODULE_ALIAS("platform:" PM8XXX_ADC_DEV_NAME);
  1125. MODULE_DESCRIPTION("PMIC8921/8018 ADC driver");
  1126. MODULE_LICENSE("GPL v2");