qcom-pm8xxx-xoadc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /*
  2. * Qualcomm PM8xxx PMIC XOADC driver
  3. *
  4. * These ADCs are known as HK/XO (house keeping / chrystal oscillator)
  5. * "XO" in "XOADC" means Chrystal Oscillator. It's a bunch of
  6. * specific-purpose and general purpose ADC converters and channels.
  7. *
  8. * Copyright (C) 2017 Linaro Ltd.
  9. * Author: Linus Walleij <linus.walleij@linaro.org>
  10. */
  11. #include <linux/iio/iio.h>
  12. #include <linux/iio/sysfs.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/regulator/consumer.h>
  21. #include "qcom-vadc-common.h"
  22. /*
  23. * Definitions for the "user processor" registers lifted from the v3.4
  24. * Qualcomm tree. Their kernel has two out-of-tree drivers for the ADC:
  25. * drivers/misc/pmic8058-xoadc.c
  26. * drivers/hwmon/pm8xxx-adc.c
  27. * None of them contain any complete register specification, so this is
  28. * a best effort of combining the information.
  29. */
  30. /* These appear to be "battery monitor" registers */
  31. #define ADC_ARB_BTM_CNTRL1 0x17e
  32. #define ADC_ARB_BTM_CNTRL1_EN_BTM BIT(0)
  33. #define ADC_ARB_BTM_CNTRL1_SEL_OP_MODE BIT(1)
  34. #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL1 BIT(2)
  35. #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL2 BIT(3)
  36. #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL3 BIT(4)
  37. #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL4 BIT(5)
  38. #define ADC_ARB_BTM_CNTRL1_EOC BIT(6)
  39. #define ADC_ARB_BTM_CNTRL1_REQ BIT(7)
  40. #define ADC_ARB_BTM_AMUX_CNTRL 0x17f
  41. #define ADC_ARB_BTM_ANA_PARAM 0x180
  42. #define ADC_ARB_BTM_DIG_PARAM 0x181
  43. #define ADC_ARB_BTM_RSV 0x182
  44. #define ADC_ARB_BTM_DATA1 0x183
  45. #define ADC_ARB_BTM_DATA0 0x184
  46. #define ADC_ARB_BTM_BAT_COOL_THR1 0x185
  47. #define ADC_ARB_BTM_BAT_COOL_THR0 0x186
  48. #define ADC_ARB_BTM_BAT_WARM_THR1 0x187
  49. #define ADC_ARB_BTM_BAT_WARM_THR0 0x188
  50. #define ADC_ARB_BTM_CNTRL2 0x18c
  51. /* Proper ADC registers */
  52. #define ADC_ARB_USRP_CNTRL 0x197
  53. #define ADC_ARB_USRP_CNTRL_EN_ARB BIT(0)
  54. #define ADC_ARB_USRP_CNTRL_RSV1 BIT(1)
  55. #define ADC_ARB_USRP_CNTRL_RSV2 BIT(2)
  56. #define ADC_ARB_USRP_CNTRL_RSV3 BIT(3)
  57. #define ADC_ARB_USRP_CNTRL_RSV4 BIT(4)
  58. #define ADC_ARB_USRP_CNTRL_RSV5 BIT(5)
  59. #define ADC_ARB_USRP_CNTRL_EOC BIT(6)
  60. #define ADC_ARB_USRP_CNTRL_REQ BIT(7)
  61. #define ADC_ARB_USRP_AMUX_CNTRL 0x198
  62. /*
  63. * The channel mask includes the bits selecting channel mux and prescaler
  64. * on PM8058, or channel mux and premux on PM8921.
  65. */
  66. #define ADC_ARB_USRP_AMUX_CNTRL_CHAN_MASK 0xfc
  67. #define ADC_ARB_USRP_AMUX_CNTRL_RSV0 BIT(0)
  68. #define ADC_ARB_USRP_AMUX_CNTRL_RSV1 BIT(1)
  69. /* On PM8058 this is prescaling, on PM8921 this is premux */
  70. #define ADC_ARB_USRP_AMUX_CNTRL_PRESCALEMUX0 BIT(2)
  71. #define ADC_ARB_USRP_AMUX_CNTRL_PRESCALEMUX1 BIT(3)
  72. #define ADC_ARB_USRP_AMUX_CNTRL_SEL0 BIT(4)
  73. #define ADC_ARB_USRP_AMUX_CNTRL_SEL1 BIT(5)
  74. #define ADC_ARB_USRP_AMUX_CNTRL_SEL2 BIT(6)
  75. #define ADC_ARB_USRP_AMUX_CNTRL_SEL3 BIT(7)
  76. #define ADC_AMUX_PREMUX_SHIFT 2
  77. #define ADC_AMUX_SEL_SHIFT 4
  78. /* We know very little about the bits in this register */
  79. #define ADC_ARB_USRP_ANA_PARAM 0x199
  80. #define ADC_ARB_USRP_ANA_PARAM_DIS 0xFE
  81. #define ADC_ARB_USRP_ANA_PARAM_EN 0xFF
  82. #define ADC_ARB_USRP_DIG_PARAM 0x19A
  83. #define ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT0 BIT(0)
  84. #define ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT1 BIT(1)
  85. #define ADC_ARB_USRP_DIG_PARAM_CLK_RATE0 BIT(2)
  86. #define ADC_ARB_USRP_DIG_PARAM_CLK_RATE1 BIT(3)
  87. #define ADC_ARB_USRP_DIG_PARAM_EOC BIT(4)
  88. /*
  89. * On a later ADC the decimation factors are defined as
  90. * 00 = 512, 01 = 1024, 10 = 2048, 11 = 4096 so assume this
  91. * holds also for this older XOADC.
  92. */
  93. #define ADC_ARB_USRP_DIG_PARAM_DEC_RATE0 BIT(5)
  94. #define ADC_ARB_USRP_DIG_PARAM_DEC_RATE1 BIT(6)
  95. #define ADC_ARB_USRP_DIG_PARAM_EN BIT(7)
  96. #define ADC_DIG_PARAM_DEC_SHIFT 5
  97. #define ADC_ARB_USRP_RSV 0x19B
  98. #define ADC_ARB_USRP_RSV_RST BIT(0)
  99. #define ADC_ARB_USRP_RSV_DTEST0 BIT(1)
  100. #define ADC_ARB_USRP_RSV_DTEST1 BIT(2)
  101. #define ADC_ARB_USRP_RSV_OP BIT(3)
  102. #define ADC_ARB_USRP_RSV_IP_SEL0 BIT(4)
  103. #define ADC_ARB_USRP_RSV_IP_SEL1 BIT(5)
  104. #define ADC_ARB_USRP_RSV_IP_SEL2 BIT(6)
  105. #define ADC_ARB_USRP_RSV_TRM BIT(7)
  106. #define ADC_RSV_IP_SEL_SHIFT 4
  107. #define ADC_ARB_USRP_DATA0 0x19D
  108. #define ADC_ARB_USRP_DATA1 0x19C
  109. /**
  110. * Physical channels which MUST exist on all PM variants in order to provide
  111. * proper reference points for calibration.
  112. *
  113. * @PM8XXX_CHANNEL_INTERNAL: 625mV reference channel
  114. * @PM8XXX_CHANNEL_125V: 1250mV reference channel
  115. * @PM8XXX_CHANNEL_INTERNAL_2: 325mV reference channel
  116. * @PM8XXX_CHANNEL_MUXOFF: channel to reduce input load on mux, apparently also
  117. * measures XO temperature
  118. */
  119. #define PM8XXX_CHANNEL_INTERNAL 0x0c
  120. #define PM8XXX_CHANNEL_125V 0x0d
  121. #define PM8XXX_CHANNEL_INTERNAL_2 0x0e
  122. #define PM8XXX_CHANNEL_MUXOFF 0x0f
  123. /*
  124. * PM8058 AMUX premux scaling, two bits. This is done of the channel before
  125. * reaching the AMUX.
  126. */
  127. #define PM8058_AMUX_PRESCALE_0 0x0 /* No scaling on the signal */
  128. #define PM8058_AMUX_PRESCALE_1 0x1 /* Unity scaling selected by the user */
  129. #define PM8058_AMUX_PRESCALE_1_DIV3 0x2 /* 1/3 prescaler on the input */
  130. /* Defines reference voltage for the XOADC */
  131. #define AMUX_RSV0 0x0 /* XO_IN/XOADC_GND, special selection to read XO temp */
  132. #define AMUX_RSV1 0x1 /* PMIC_IN/XOADC_GND */
  133. #define AMUX_RSV2 0x2 /* PMIC_IN/BMS_CSP */
  134. #define AMUX_RSV3 0x3 /* not used */
  135. #define AMUX_RSV4 0x4 /* XOADC_GND/XOADC_GND */
  136. #define AMUX_RSV5 0x5 /* XOADC_VREF/XOADC_GND */
  137. #define XOADC_RSV_MAX 5 /* 3 bits 0..7, 3 and 6,7 are invalid */
  138. /**
  139. * struct xoadc_channel - encodes channel properties and defaults
  140. * @datasheet_name: the hardwarename of this channel
  141. * @pre_scale_mux: prescale (PM8058) or premux (PM8921) for selecting
  142. * this channel. Both this and the amux channel is needed to uniquely
  143. * identify a channel. Values 0..3.
  144. * @amux_channel: value of the ADC_ARB_USRP_AMUX_CNTRL register for this
  145. * channel, bits 4..7, selects the amux, values 0..f
  146. * @prescale: the channels have hard-coded prescale ratios defined
  147. * by the hardware, this tells us what it is
  148. * @type: corresponding IIO channel type, usually IIO_VOLTAGE or
  149. * IIO_TEMP
  150. * @scale_fn_type: the liner interpolation etc to convert the
  151. * ADC code to the value that IIO expects, in uV or millicelsius
  152. * etc. This scale function can be pretty elaborate if different
  153. * thermistors are connected or other hardware characteristics are
  154. * deployed.
  155. * @amux_ip_rsv: ratiometric scale value used by the analog muxer: this
  156. * selects the reference voltage for ratiometric scaling
  157. */
  158. struct xoadc_channel {
  159. const char *datasheet_name;
  160. u8 pre_scale_mux:2;
  161. u8 amux_channel:4;
  162. const struct vadc_prescale_ratio prescale;
  163. enum iio_chan_type type;
  164. enum vadc_scale_fn_type scale_fn_type;
  165. u8 amux_ip_rsv:3;
  166. };
  167. /**
  168. * struct xoadc_variant - encodes the XOADC variant characteristics
  169. * @name: name of this PMIC variant
  170. * @channels: the hardware channels and respective settings and defaults
  171. * @broken_ratiometric: if the PMIC has broken ratiometric scaling (this
  172. * is a known problem on PM8058)
  173. * @prescaling: this variant uses AMUX bits 2 & 3 for prescaling (PM8058)
  174. * @second_level_mux: this variant uses AMUX bits 2 & 3 for a second level
  175. * mux
  176. */
  177. struct xoadc_variant {
  178. const char name[16];
  179. const struct xoadc_channel *channels;
  180. bool broken_ratiometric;
  181. bool prescaling;
  182. bool second_level_mux;
  183. };
  184. /*
  185. * XOADC_CHAN macro parameters:
  186. * _dname: the name of the channel
  187. * _presmux: prescaler (PM8058) or premux (PM8921) setting for this channel
  188. * _amux: the value in bits 2..7 of the ADC_ARB_USRP_AMUX_CNTRL register
  189. * for this channel. On some PMICs some of the bits select a prescaler, and
  190. * on some PMICs some of the bits select various complex multiplex settings.
  191. * _type: IIO channel type
  192. * _prenum: prescaler numerator (dividend)
  193. * _preden: prescaler denominator (divisor)
  194. * _scale: scaling function type, this selects how the raw valued is mangled
  195. * to output the actual processed measurement
  196. * _amip: analog mux input parent when using ratiometric measurements
  197. */
  198. #define XOADC_CHAN(_dname, _presmux, _amux, _type, _prenum, _preden, _scale, _amip) \
  199. { \
  200. .datasheet_name = __stringify(_dname), \
  201. .pre_scale_mux = _presmux, \
  202. .amux_channel = _amux, \
  203. .prescale = { .num = _prenum, .den = _preden }, \
  204. .type = _type, \
  205. .scale_fn_type = _scale, \
  206. .amux_ip_rsv = _amip, \
  207. }
  208. /*
  209. * Taken from arch/arm/mach-msm/board-9615.c in the vendor tree:
  210. * TODO: incomplete, needs testing.
  211. */
  212. static const struct xoadc_channel pm8018_xoadc_channels[] = {
  213. XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  214. XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  215. XOADC_CHAN(VPH_PWR, 0x00, 0x02, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  216. XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
  217. /* Used for battery ID or battery temperature */
  218. XOADC_CHAN(AMUX8, 0x00, 0x08, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV2),
  219. XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  220. XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  221. XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
  222. { }, /* Sentinel */
  223. };
  224. /*
  225. * Taken from arch/arm/mach-msm/board-8930-pmic.c in the vendor tree:
  226. * TODO: needs testing.
  227. */
  228. static const struct xoadc_channel pm8038_xoadc_channels[] = {
  229. XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  230. XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  231. XOADC_CHAN(DCIN, 0x00, 0x02, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
  232. XOADC_CHAN(ICHG, 0x00, 0x03, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  233. XOADC_CHAN(VPH_PWR, 0x00, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  234. XOADC_CHAN(AMUX5, 0x00, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  235. XOADC_CHAN(AMUX6, 0x00, 0x06, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  236. XOADC_CHAN(AMUX7, 0x00, 0x07, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  237. /* AMUX8 used for battery temperature in most cases */
  238. XOADC_CHAN(AMUX8, 0x00, 0x08, IIO_TEMP, 1, 1, SCALE_THERM_100K_PULLUP, AMUX_RSV2),
  239. XOADC_CHAN(AMUX9, 0x00, 0x09, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  240. XOADC_CHAN(USB_VBUS, 0x00, 0x0a, IIO_VOLTAGE, 1, 4, SCALE_DEFAULT, AMUX_RSV1),
  241. XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
  242. XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  243. XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  244. XOADC_CHAN(INTERNAL_2, 0x00, 0x0e, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  245. XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
  246. { }, /* Sentinel */
  247. };
  248. /*
  249. * This was created by cross-referencing the vendor tree
  250. * arch/arm/mach-msm/board-msm8x60.c msm_adc_channels_data[]
  251. * with the "channel types" (first field) to find the right
  252. * configuration for these channels on an MSM8x60 i.e. PM8058
  253. * setup.
  254. */
  255. static const struct xoadc_channel pm8058_xoadc_channels[] = {
  256. XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
  257. XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  258. XOADC_CHAN(DCIN, 0x00, 0x02, IIO_VOLTAGE, 1, 10, SCALE_DEFAULT, AMUX_RSV1),
  259. XOADC_CHAN(ICHG, 0x00, 0x03, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  260. XOADC_CHAN(VPH_PWR, 0x00, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  261. /*
  262. * AMUX channels 5 thru 9 are referred to as MPP5 thru MPP9 in
  263. * some code and documentation. But they are really just 5
  264. * channels just like any other. They are connected to a switching
  265. * matrix where they can be routed to any of the MPPs, not just
  266. * 1-to-1 onto MPP5 thru 9, so naming them MPP5 thru MPP9 is
  267. * very confusing.
  268. */
  269. XOADC_CHAN(AMUX5, 0x00, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  270. XOADC_CHAN(AMUX6, 0x00, 0x06, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  271. XOADC_CHAN(AMUX7, 0x00, 0x07, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
  272. XOADC_CHAN(AMUX8, 0x00, 0x08, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
  273. XOADC_CHAN(AMUX9, 0x00, 0x09, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  274. XOADC_CHAN(USB_VBUS, 0x00, 0x0a, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  275. XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
  276. XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  277. XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  278. XOADC_CHAN(INTERNAL_2, 0x00, 0x0e, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  279. XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
  280. /* There are also "unity" and divided by 3 channels (prescaler) but noone is using them */
  281. { }, /* Sentinel */
  282. };
  283. /*
  284. * The PM8921 has some pre-muxing on its channels, this comes from the vendor tree
  285. * include/linux/mfd/pm8xxx/pm8xxx-adc.h
  286. * board-flo-pmic.c (Nexus 7) and board-8064-pmic.c
  287. */
  288. static const struct xoadc_channel pm8921_xoadc_channels[] = {
  289. XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  290. XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  291. XOADC_CHAN(DCIN, 0x00, 0x02, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
  292. /* channel "ICHG" is reserved and not used on PM8921 */
  293. XOADC_CHAN(VPH_PWR, 0x00, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  294. XOADC_CHAN(IBAT, 0x00, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  295. /* CHAN 6 & 7 (MPP1 & MPP2) are reserved for MPP channels on PM8921 */
  296. XOADC_CHAN(BATT_THERM, 0x00, 0x08, IIO_TEMP, 1, 1, SCALE_THERM_100K_PULLUP, AMUX_RSV1),
  297. XOADC_CHAN(BATT_ID, 0x00, 0x09, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  298. XOADC_CHAN(USB_VBUS, 0x00, 0x0a, IIO_VOLTAGE, 1, 4, SCALE_DEFAULT, AMUX_RSV1),
  299. XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
  300. XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  301. XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  302. /* FIXME: look into the scaling of this temperature */
  303. XOADC_CHAN(CHG_TEMP, 0x00, 0x0e, IIO_TEMP, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  304. XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
  305. /* The following channels have premux bit 0 set to 1 (all end in 4) */
  306. XOADC_CHAN(ATEST_8, 0x01, 0x00, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  307. /* Set scaling to 1/2 based on the name for these two */
  308. XOADC_CHAN(USB_SNS_DIV20, 0x01, 0x01, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
  309. XOADC_CHAN(DCIN_SNS_DIV20, 0x01, 0x02, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
  310. XOADC_CHAN(AMUX3, 0x01, 0x03, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  311. XOADC_CHAN(AMUX4, 0x01, 0x04, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  312. XOADC_CHAN(AMUX5, 0x01, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  313. XOADC_CHAN(AMUX6, 0x01, 0x06, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  314. XOADC_CHAN(AMUX7, 0x01, 0x07, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  315. XOADC_CHAN(AMUX8, 0x01, 0x08, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  316. /* Internal test signals, I think */
  317. XOADC_CHAN(ATEST_1, 0x01, 0x09, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  318. XOADC_CHAN(ATEST_2, 0x01, 0x0a, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  319. XOADC_CHAN(ATEST_3, 0x01, 0x0b, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  320. XOADC_CHAN(ATEST_4, 0x01, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  321. XOADC_CHAN(ATEST_5, 0x01, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  322. XOADC_CHAN(ATEST_6, 0x01, 0x0e, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  323. XOADC_CHAN(ATEST_7, 0x01, 0x0f, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
  324. /* The following channels have premux bit 1 set to 1 (all end in 8) */
  325. /* I guess even ATEST8 will be divided by 3 here */
  326. XOADC_CHAN(ATEST_8, 0x02, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  327. /* I guess div 2 div 3 becomes div 6 */
  328. XOADC_CHAN(USB_SNS_DIV20_DIV3, 0x02, 0x01, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
  329. XOADC_CHAN(DCIN_SNS_DIV20_DIV3, 0x02, 0x02, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
  330. XOADC_CHAN(AMUX3_DIV3, 0x02, 0x03, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  331. XOADC_CHAN(AMUX4_DIV3, 0x02, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  332. XOADC_CHAN(AMUX5_DIV3, 0x02, 0x05, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  333. XOADC_CHAN(AMUX6_DIV3, 0x02, 0x06, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  334. XOADC_CHAN(AMUX7_DIV3, 0x02, 0x07, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  335. XOADC_CHAN(AMUX8_DIV3, 0x02, 0x08, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  336. XOADC_CHAN(ATEST_1_DIV3, 0x02, 0x09, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  337. XOADC_CHAN(ATEST_2_DIV3, 0x02, 0x0a, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  338. XOADC_CHAN(ATEST_3_DIV3, 0x02, 0x0b, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  339. XOADC_CHAN(ATEST_4_DIV3, 0x02, 0x0c, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  340. XOADC_CHAN(ATEST_5_DIV3, 0x02, 0x0d, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  341. XOADC_CHAN(ATEST_6_DIV3, 0x02, 0x0e, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  342. XOADC_CHAN(ATEST_7_DIV3, 0x02, 0x0f, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
  343. { }, /* Sentinel */
  344. };
  345. /**
  346. * struct pm8xxx_chan_info - ADC channel information
  347. * @name: name of this channel
  348. * @hwchan: pointer to hardware channel information (muxing & scaling settings)
  349. * @calibration: whether to use absolute or ratiometric calibration
  350. * @scale_fn_type: scaling function type
  351. * @decimation: 0,1,2,3
  352. * @amux_ip_rsv: ratiometric scale value if using ratiometric
  353. * calibration: 0, 1, 2, 4, 5.
  354. */
  355. struct pm8xxx_chan_info {
  356. const char *name;
  357. const struct xoadc_channel *hwchan;
  358. enum vadc_calibration calibration;
  359. u8 decimation:2;
  360. u8 amux_ip_rsv:3;
  361. };
  362. /**
  363. * struct pm8xxx_xoadc - state container for the XOADC
  364. * @dev: pointer to device
  365. * @map: regmap to access registers
  366. * @vref: reference voltage regulator
  367. * characteristics of the channels, and sensible default settings
  368. * @nchans: number of channels, configured by the device tree
  369. * @chans: the channel information per-channel, configured by the device tree
  370. * @iio_chans: IIO channel specifiers
  371. * @graph: linear calibration parameters for absolute and
  372. * ratiometric measurements
  373. * @complete: completion to indicate end of conversion
  374. * @lock: lock to restrict access to the hardware to one client at the time
  375. */
  376. struct pm8xxx_xoadc {
  377. struct device *dev;
  378. struct regmap *map;
  379. const struct xoadc_variant *variant;
  380. struct regulator *vref;
  381. unsigned int nchans;
  382. struct pm8xxx_chan_info *chans;
  383. struct iio_chan_spec *iio_chans;
  384. struct vadc_linear_graph graph[2];
  385. struct completion complete;
  386. struct mutex lock;
  387. };
  388. static irqreturn_t pm8xxx_eoc_irq(int irq, void *d)
  389. {
  390. struct iio_dev *indio_dev = d;
  391. struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
  392. complete(&adc->complete);
  393. return IRQ_HANDLED;
  394. }
  395. static struct pm8xxx_chan_info *
  396. pm8xxx_get_channel(struct pm8xxx_xoadc *adc, u8 chan)
  397. {
  398. struct pm8xxx_chan_info *ch;
  399. int i;
  400. for (i = 0; i < adc->nchans; i++) {
  401. ch = &adc->chans[i];
  402. if (ch->hwchan->amux_channel == chan)
  403. break;
  404. }
  405. if (i == adc->nchans)
  406. return NULL;
  407. return ch;
  408. }
  409. static int pm8xxx_read_channel_rsv(struct pm8xxx_xoadc *adc,
  410. const struct pm8xxx_chan_info *ch,
  411. u8 rsv, u16 *adc_code,
  412. bool force_ratiometric)
  413. {
  414. int ret;
  415. unsigned int val;
  416. u8 rsvmask, rsvval;
  417. u8 lsb, msb;
  418. dev_dbg(adc->dev, "read channel \"%s\", amux %d, prescale/mux: %d, rsv %d\n",
  419. ch->name, ch->hwchan->amux_channel, ch->hwchan->pre_scale_mux, rsv);
  420. mutex_lock(&adc->lock);
  421. /* Mux in this channel */
  422. val = ch->hwchan->amux_channel << ADC_AMUX_SEL_SHIFT;
  423. val |= ch->hwchan->pre_scale_mux << ADC_AMUX_PREMUX_SHIFT;
  424. ret = regmap_write(adc->map, ADC_ARB_USRP_AMUX_CNTRL, val);
  425. if (ret)
  426. goto unlock;
  427. /* Set up ratiometric scale value, mask off all bits except these */
  428. rsvmask = (ADC_ARB_USRP_RSV_RST | ADC_ARB_USRP_RSV_DTEST0 |
  429. ADC_ARB_USRP_RSV_DTEST1 | ADC_ARB_USRP_RSV_OP);
  430. if (adc->variant->broken_ratiometric && !force_ratiometric) {
  431. /*
  432. * Apparently the PM8058 has some kind of bug which is
  433. * reflected in the vendor tree drivers/misc/pmix8058-xoadc.c
  434. * which just hardcodes the RSV selector to SEL1 (0x20) for
  435. * most cases and SEL0 (0x10) for the MUXOFF channel only.
  436. * If we force ratiometric (currently only done when attempting
  437. * to do ratiometric calibration) this doesn't seem to work
  438. * very well and I suspect ratiometric conversion is simply
  439. * broken or not supported on the PM8058.
  440. *
  441. * Maybe IO_SEL2 doesn't exist on PM8058 and bits 4 & 5 select
  442. * the mode alone.
  443. *
  444. * Some PM8058 register documentation would be nice to get
  445. * this right.
  446. */
  447. if (ch->hwchan->amux_channel == PM8XXX_CHANNEL_MUXOFF)
  448. rsvval = ADC_ARB_USRP_RSV_IP_SEL0;
  449. else
  450. rsvval = ADC_ARB_USRP_RSV_IP_SEL1;
  451. } else {
  452. if (rsv == 0xff)
  453. rsvval = (ch->amux_ip_rsv << ADC_RSV_IP_SEL_SHIFT) |
  454. ADC_ARB_USRP_RSV_TRM;
  455. else
  456. rsvval = (rsv << ADC_RSV_IP_SEL_SHIFT) |
  457. ADC_ARB_USRP_RSV_TRM;
  458. }
  459. ret = regmap_update_bits(adc->map,
  460. ADC_ARB_USRP_RSV,
  461. ~rsvmask,
  462. rsvval);
  463. if (ret)
  464. goto unlock;
  465. ret = regmap_write(adc->map, ADC_ARB_USRP_ANA_PARAM,
  466. ADC_ARB_USRP_ANA_PARAM_DIS);
  467. if (ret)
  468. goto unlock;
  469. /* Decimation factor */
  470. ret = regmap_write(adc->map, ADC_ARB_USRP_DIG_PARAM,
  471. ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT0 |
  472. ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT1 |
  473. ch->decimation << ADC_DIG_PARAM_DEC_SHIFT);
  474. if (ret)
  475. goto unlock;
  476. ret = regmap_write(adc->map, ADC_ARB_USRP_ANA_PARAM,
  477. ADC_ARB_USRP_ANA_PARAM_EN);
  478. if (ret)
  479. goto unlock;
  480. /* Enable the arbiter, the Qualcomm code does it twice like this */
  481. ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL,
  482. ADC_ARB_USRP_CNTRL_EN_ARB);
  483. if (ret)
  484. goto unlock;
  485. ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL,
  486. ADC_ARB_USRP_CNTRL_EN_ARB);
  487. if (ret)
  488. goto unlock;
  489. /* Fire a request! */
  490. reinit_completion(&adc->complete);
  491. ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL,
  492. ADC_ARB_USRP_CNTRL_EN_ARB |
  493. ADC_ARB_USRP_CNTRL_REQ);
  494. if (ret)
  495. goto unlock;
  496. /* Next the interrupt occurs */
  497. ret = wait_for_completion_timeout(&adc->complete,
  498. VADC_CONV_TIME_MAX_US);
  499. if (!ret) {
  500. dev_err(adc->dev, "conversion timed out\n");
  501. ret = -ETIMEDOUT;
  502. goto unlock;
  503. }
  504. ret = regmap_read(adc->map, ADC_ARB_USRP_DATA0, &val);
  505. if (ret)
  506. goto unlock;
  507. lsb = val;
  508. ret = regmap_read(adc->map, ADC_ARB_USRP_DATA1, &val);
  509. if (ret)
  510. goto unlock;
  511. msb = val;
  512. *adc_code = (msb << 8) | lsb;
  513. /* Turn off the ADC by setting the arbiter to 0 twice */
  514. ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL, 0);
  515. if (ret)
  516. goto unlock;
  517. ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL, 0);
  518. if (ret)
  519. goto unlock;
  520. unlock:
  521. mutex_unlock(&adc->lock);
  522. return ret;
  523. }
  524. static int pm8xxx_read_channel(struct pm8xxx_xoadc *adc,
  525. const struct pm8xxx_chan_info *ch,
  526. u16 *adc_code)
  527. {
  528. /*
  529. * Normally we just use the ratiometric scale value (RSV) predefined
  530. * for the channel, but during calibration we need to modify this
  531. * so this wrapper is a helper hiding the more complex version.
  532. */
  533. return pm8xxx_read_channel_rsv(adc, ch, 0xff, adc_code, false);
  534. }
  535. static int pm8xxx_calibrate_device(struct pm8xxx_xoadc *adc)
  536. {
  537. const struct pm8xxx_chan_info *ch;
  538. u16 read_1250v;
  539. u16 read_0625v;
  540. u16 read_nomux_rsv5;
  541. u16 read_nomux_rsv4;
  542. int ret;
  543. adc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV;
  544. adc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE;
  545. /* Common reference channel calibration */
  546. ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_125V);
  547. if (!ch)
  548. return -ENODEV;
  549. ret = pm8xxx_read_channel(adc, ch, &read_1250v);
  550. if (ret) {
  551. dev_err(adc->dev, "could not read 1.25V reference channel\n");
  552. return -ENODEV;
  553. }
  554. ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_INTERNAL);
  555. if (!ch)
  556. return -ENODEV;
  557. ret = pm8xxx_read_channel(adc, ch, &read_0625v);
  558. if (ret) {
  559. dev_err(adc->dev, "could not read 0.625V reference channel\n");
  560. return -ENODEV;
  561. }
  562. if (read_1250v == read_0625v) {
  563. dev_err(adc->dev, "read same ADC code for 1.25V and 0.625V\n");
  564. return -ENODEV;
  565. }
  566. adc->graph[VADC_CALIB_ABSOLUTE].dy = read_1250v - read_0625v;
  567. adc->graph[VADC_CALIB_ABSOLUTE].gnd = read_0625v;
  568. dev_info(adc->dev, "absolute calibration dx = %d uV, dy = %d units\n",
  569. VADC_ABSOLUTE_RANGE_UV, adc->graph[VADC_CALIB_ABSOLUTE].dy);
  570. /* Ratiometric calibration */
  571. ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_MUXOFF);
  572. if (!ch)
  573. return -ENODEV;
  574. ret = pm8xxx_read_channel_rsv(adc, ch, AMUX_RSV5,
  575. &read_nomux_rsv5, true);
  576. if (ret) {
  577. dev_err(adc->dev, "could not read MUXOFF reference channel\n");
  578. return -ENODEV;
  579. }
  580. ret = pm8xxx_read_channel_rsv(adc, ch, AMUX_RSV4,
  581. &read_nomux_rsv4, true);
  582. if (ret) {
  583. dev_err(adc->dev, "could not read MUXOFF reference channel\n");
  584. return -ENODEV;
  585. }
  586. adc->graph[VADC_CALIB_RATIOMETRIC].dy =
  587. read_nomux_rsv5 - read_nomux_rsv4;
  588. adc->graph[VADC_CALIB_RATIOMETRIC].gnd = read_nomux_rsv4;
  589. dev_info(adc->dev, "ratiometric calibration dx = %d, dy = %d units\n",
  590. VADC_RATIOMETRIC_RANGE,
  591. adc->graph[VADC_CALIB_RATIOMETRIC].dy);
  592. return 0;
  593. }
  594. static int pm8xxx_read_raw(struct iio_dev *indio_dev,
  595. struct iio_chan_spec const *chan,
  596. int *val, int *val2, long mask)
  597. {
  598. struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
  599. const struct pm8xxx_chan_info *ch;
  600. u16 adc_code;
  601. int ret;
  602. switch (mask) {
  603. case IIO_CHAN_INFO_PROCESSED:
  604. ch = pm8xxx_get_channel(adc, chan->address);
  605. if (!ch) {
  606. dev_err(adc->dev, "no such channel %lu\n",
  607. chan->address);
  608. return -EINVAL;
  609. }
  610. ret = pm8xxx_read_channel(adc, ch, &adc_code);
  611. if (ret)
  612. return ret;
  613. ret = qcom_vadc_scale(ch->hwchan->scale_fn_type,
  614. &adc->graph[ch->calibration],
  615. &ch->hwchan->prescale,
  616. (ch->calibration == VADC_CALIB_ABSOLUTE),
  617. adc_code, val);
  618. if (ret)
  619. return ret;
  620. return IIO_VAL_INT;
  621. case IIO_CHAN_INFO_RAW:
  622. ch = pm8xxx_get_channel(adc, chan->address);
  623. if (!ch) {
  624. dev_err(adc->dev, "no such channel %lu\n",
  625. chan->address);
  626. return -EINVAL;
  627. }
  628. ret = pm8xxx_read_channel(adc, ch, &adc_code);
  629. if (ret)
  630. return ret;
  631. *val = (int)adc_code;
  632. return IIO_VAL_INT;
  633. default:
  634. return -EINVAL;
  635. }
  636. }
  637. static int pm8xxx_of_xlate(struct iio_dev *indio_dev,
  638. const struct of_phandle_args *iiospec)
  639. {
  640. struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
  641. u8 pre_scale_mux;
  642. u8 amux_channel;
  643. unsigned int i;
  644. /*
  645. * First cell is prescaler or premux, second cell is analog
  646. * mux.
  647. */
  648. if (iiospec->args_count != 2) {
  649. dev_err(&indio_dev->dev, "wrong number of arguments for %pOFn need 2 got %d\n",
  650. iiospec->np,
  651. iiospec->args_count);
  652. return -EINVAL;
  653. }
  654. pre_scale_mux = (u8)iiospec->args[0];
  655. amux_channel = (u8)iiospec->args[1];
  656. dev_dbg(&indio_dev->dev, "pre scale/mux: %02x, amux: %02x\n",
  657. pre_scale_mux, amux_channel);
  658. /* We need to match exactly on the prescale/premux and channel */
  659. for (i = 0; i < adc->nchans; i++)
  660. if (adc->chans[i].hwchan->pre_scale_mux == pre_scale_mux &&
  661. adc->chans[i].hwchan->amux_channel == amux_channel)
  662. return i;
  663. return -EINVAL;
  664. }
  665. static const struct iio_info pm8xxx_xoadc_info = {
  666. .of_xlate = pm8xxx_of_xlate,
  667. .read_raw = pm8xxx_read_raw,
  668. };
  669. static int pm8xxx_xoadc_parse_channel(struct device *dev,
  670. struct device_node *np,
  671. const struct xoadc_channel *hw_channels,
  672. struct iio_chan_spec *iio_chan,
  673. struct pm8xxx_chan_info *ch)
  674. {
  675. const char *name = np->name;
  676. const struct xoadc_channel *hwchan;
  677. u32 pre_scale_mux, amux_channel;
  678. u32 rsv, dec;
  679. int ret;
  680. int chid;
  681. ret = of_property_read_u32_index(np, "reg", 0, &pre_scale_mux);
  682. if (ret) {
  683. dev_err(dev, "invalid pre scale/mux number %s\n", name);
  684. return ret;
  685. }
  686. ret = of_property_read_u32_index(np, "reg", 1, &amux_channel);
  687. if (ret) {
  688. dev_err(dev, "invalid amux channel number %s\n", name);
  689. return ret;
  690. }
  691. /* Find the right channel setting */
  692. chid = 0;
  693. hwchan = &hw_channels[0];
  694. while (hwchan && hwchan->datasheet_name) {
  695. if (hwchan->pre_scale_mux == pre_scale_mux &&
  696. hwchan->amux_channel == amux_channel)
  697. break;
  698. hwchan++;
  699. chid++;
  700. }
  701. /* The sentinel does not have a name assigned */
  702. if (!hwchan->datasheet_name) {
  703. dev_err(dev, "could not locate channel %02x/%02x\n",
  704. pre_scale_mux, amux_channel);
  705. return -EINVAL;
  706. }
  707. ch->name = name;
  708. ch->hwchan = hwchan;
  709. /* Everyone seems to use absolute calibration except in special cases */
  710. ch->calibration = VADC_CALIB_ABSOLUTE;
  711. /* Everyone seems to use default ("type 2") decimation */
  712. ch->decimation = VADC_DEF_DECIMATION;
  713. if (!of_property_read_u32(np, "qcom,ratiometric", &rsv)) {
  714. ch->calibration = VADC_CALIB_RATIOMETRIC;
  715. if (rsv > XOADC_RSV_MAX) {
  716. dev_err(dev, "%s too large RSV value %d\n", name, rsv);
  717. return -EINVAL;
  718. }
  719. if (rsv == AMUX_RSV3) {
  720. dev_err(dev, "%s invalid RSV value %d\n", name, rsv);
  721. return -EINVAL;
  722. }
  723. }
  724. /* Optional decimation, if omitted we use the default */
  725. ret = of_property_read_u32(np, "qcom,decimation", &dec);
  726. if (!ret) {
  727. ret = qcom_vadc_decimation_from_dt(dec);
  728. if (ret < 0) {
  729. dev_err(dev, "%s invalid decimation %d\n",
  730. name, dec);
  731. return ret;
  732. }
  733. ch->decimation = ret;
  734. }
  735. iio_chan->channel = chid;
  736. iio_chan->address = hwchan->amux_channel;
  737. iio_chan->datasheet_name = hwchan->datasheet_name;
  738. iio_chan->type = hwchan->type;
  739. /* All channels are raw or processed */
  740. iio_chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  741. BIT(IIO_CHAN_INFO_PROCESSED);
  742. iio_chan->indexed = 1;
  743. dev_dbg(dev, "channel [PRESCALE/MUX: %02x AMUX: %02x] \"%s\" "
  744. "ref voltage: %d, decimation %d "
  745. "prescale %d/%d, scale function %d\n",
  746. hwchan->pre_scale_mux, hwchan->amux_channel, ch->name,
  747. ch->amux_ip_rsv, ch->decimation, hwchan->prescale.num,
  748. hwchan->prescale.den, hwchan->scale_fn_type);
  749. return 0;
  750. }
  751. static int pm8xxx_xoadc_parse_channels(struct pm8xxx_xoadc *adc,
  752. struct device_node *np)
  753. {
  754. struct device_node *child;
  755. struct pm8xxx_chan_info *ch;
  756. int ret;
  757. int i;
  758. adc->nchans = of_get_available_child_count(np);
  759. if (!adc->nchans) {
  760. dev_err(adc->dev, "no channel children\n");
  761. return -ENODEV;
  762. }
  763. dev_dbg(adc->dev, "found %d ADC channels\n", adc->nchans);
  764. adc->iio_chans = devm_kcalloc(adc->dev, adc->nchans,
  765. sizeof(*adc->iio_chans), GFP_KERNEL);
  766. if (!adc->iio_chans)
  767. return -ENOMEM;
  768. adc->chans = devm_kcalloc(adc->dev, adc->nchans,
  769. sizeof(*adc->chans), GFP_KERNEL);
  770. if (!adc->chans)
  771. return -ENOMEM;
  772. i = 0;
  773. for_each_available_child_of_node(np, child) {
  774. ch = &adc->chans[i];
  775. ret = pm8xxx_xoadc_parse_channel(adc->dev, child,
  776. adc->variant->channels,
  777. &adc->iio_chans[i],
  778. ch);
  779. if (ret) {
  780. of_node_put(child);
  781. return ret;
  782. }
  783. i++;
  784. }
  785. /* Check for required channels */
  786. ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_125V);
  787. if (!ch) {
  788. dev_err(adc->dev, "missing 1.25V reference channel\n");
  789. return -ENODEV;
  790. }
  791. ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_INTERNAL);
  792. if (!ch) {
  793. dev_err(adc->dev, "missing 0.625V reference channel\n");
  794. return -ENODEV;
  795. }
  796. ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_MUXOFF);
  797. if (!ch) {
  798. dev_err(adc->dev, "missing MUXOFF reference channel\n");
  799. return -ENODEV;
  800. }
  801. return 0;
  802. }
  803. static int pm8xxx_xoadc_probe(struct platform_device *pdev)
  804. {
  805. const struct xoadc_variant *variant;
  806. struct pm8xxx_xoadc *adc;
  807. struct iio_dev *indio_dev;
  808. struct device_node *np = pdev->dev.of_node;
  809. struct regmap *map;
  810. struct device *dev = &pdev->dev;
  811. int ret;
  812. variant = of_device_get_match_data(dev);
  813. if (!variant)
  814. return -ENODEV;
  815. indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
  816. if (!indio_dev)
  817. return -ENOMEM;
  818. platform_set_drvdata(pdev, indio_dev);
  819. adc = iio_priv(indio_dev);
  820. adc->dev = dev;
  821. adc->variant = variant;
  822. init_completion(&adc->complete);
  823. mutex_init(&adc->lock);
  824. ret = pm8xxx_xoadc_parse_channels(adc, np);
  825. if (ret)
  826. return ret;
  827. map = dev_get_regmap(dev->parent, NULL);
  828. if (!map) {
  829. dev_err(dev, "parent regmap unavailable.\n");
  830. return -ENXIO;
  831. }
  832. adc->map = map;
  833. /* Bring up regulator */
  834. adc->vref = devm_regulator_get(dev, "xoadc-ref");
  835. if (IS_ERR(adc->vref)) {
  836. dev_err(dev, "failed to get XOADC VREF regulator\n");
  837. return PTR_ERR(adc->vref);
  838. }
  839. ret = regulator_enable(adc->vref);
  840. if (ret) {
  841. dev_err(dev, "failed to enable XOADC VREF regulator\n");
  842. return ret;
  843. }
  844. ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
  845. pm8xxx_eoc_irq, NULL, 0, variant->name, indio_dev);
  846. if (ret) {
  847. dev_err(dev, "unable to request IRQ\n");
  848. goto out_disable_vref;
  849. }
  850. indio_dev->dev.parent = dev;
  851. indio_dev->dev.of_node = np;
  852. indio_dev->name = variant->name;
  853. indio_dev->modes = INDIO_DIRECT_MODE;
  854. indio_dev->info = &pm8xxx_xoadc_info;
  855. indio_dev->channels = adc->iio_chans;
  856. indio_dev->num_channels = adc->nchans;
  857. ret = iio_device_register(indio_dev);
  858. if (ret)
  859. goto out_disable_vref;
  860. ret = pm8xxx_calibrate_device(adc);
  861. if (ret)
  862. goto out_unreg_device;
  863. dev_info(dev, "%s XOADC driver enabled\n", variant->name);
  864. return 0;
  865. out_unreg_device:
  866. iio_device_unregister(indio_dev);
  867. out_disable_vref:
  868. regulator_disable(adc->vref);
  869. return ret;
  870. }
  871. static int pm8xxx_xoadc_remove(struct platform_device *pdev)
  872. {
  873. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  874. struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
  875. iio_device_unregister(indio_dev);
  876. regulator_disable(adc->vref);
  877. return 0;
  878. }
  879. static const struct xoadc_variant pm8018_variant = {
  880. .name = "PM8018-XOADC",
  881. .channels = pm8018_xoadc_channels,
  882. };
  883. static const struct xoadc_variant pm8038_variant = {
  884. .name = "PM8038-XOADC",
  885. .channels = pm8038_xoadc_channels,
  886. };
  887. static const struct xoadc_variant pm8058_variant = {
  888. .name = "PM8058-XOADC",
  889. .channels = pm8058_xoadc_channels,
  890. .broken_ratiometric = true,
  891. .prescaling = true,
  892. };
  893. static const struct xoadc_variant pm8921_variant = {
  894. .name = "PM8921-XOADC",
  895. .channels = pm8921_xoadc_channels,
  896. .second_level_mux = true,
  897. };
  898. static const struct of_device_id pm8xxx_xoadc_id_table[] = {
  899. {
  900. .compatible = "qcom,pm8018-adc",
  901. .data = &pm8018_variant,
  902. },
  903. {
  904. .compatible = "qcom,pm8038-adc",
  905. .data = &pm8038_variant,
  906. },
  907. {
  908. .compatible = "qcom,pm8058-adc",
  909. .data = &pm8058_variant,
  910. },
  911. {
  912. .compatible = "qcom,pm8921-adc",
  913. .data = &pm8921_variant,
  914. },
  915. { },
  916. };
  917. MODULE_DEVICE_TABLE(of, pm8xxx_xoadc_id_table);
  918. static struct platform_driver pm8xxx_xoadc_driver = {
  919. .driver = {
  920. .name = "pm8xxx-adc",
  921. .of_match_table = pm8xxx_xoadc_id_table,
  922. },
  923. .probe = pm8xxx_xoadc_probe,
  924. .remove = pm8xxx_xoadc_remove,
  925. };
  926. module_platform_driver(pm8xxx_xoadc_driver);
  927. MODULE_DESCRIPTION("PM8xxx XOADC driver");
  928. MODULE_LICENSE("GPL v2");
  929. MODULE_ALIAS("platform:pm8xxx-xoadc");