qcom-spmi-adc5-gen3.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/completion.h>
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/log2.h>
  13. #include <linux/math64.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/thermal.h>
  21. #include <linux/thermal_minidump.h>
  22. #include <linux/slab.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/adc/qcom-vadc-common.h>
  25. #include <dt-bindings/iio/qcom,spmi-vadc.h>
  26. static LIST_HEAD(adc_tm_device_list);
  27. #define ADC5_GEN3_HS 0x45
  28. #define ADC5_GEN3_HS_BUSY BIT(7)
  29. #define ADC5_GEN3_HS_READY BIT(0)
  30. #define ADC5_GEN3_STATUS1 0x46
  31. #define ADC5_GEN3_STATUS1_CONV_FAULT BIT(7)
  32. #define ADC5_GEN3_STATUS1_THR_CROSS BIT(6)
  33. #define ADC5_GEN3_STATUS1_EOC BIT(0)
  34. #define ADC5_GEN3_TM_EN_STS 0x47
  35. #define ADC5_GEN3_TM_HIGH_STS 0x48
  36. #define ADC5_GEN3_TM_LOW_STS 0x49
  37. #define ADC5_GEN3_EOC_STS 0x4a
  38. #define ADC5_GEN3_EOC_CHAN_0 BIT(0)
  39. #define ADC5_GEN3_EOC_CLR 0x4b
  40. #define ADC5_GEN3_TM_HIGH_STS_CLR 0x4c
  41. #define ADC5_GEN3_TM_LOW_STS_CLR 0x4d
  42. #define ADC5_GEN3_CONV_ERR_CLR 0x4e
  43. #define ADC5_GEN3_CONV_ERR_CLR_REQ BIT(0)
  44. #define ADC5_GEN3_SID 0x4f
  45. #define ADC5_GEN3_SID_MASK GENMASK(3, 0)
  46. #define ADC5_GEN3_PERPH_CH 0x50
  47. #define ADC5_GEN3_CHAN_CONV_REQ BIT(7)
  48. #define ADC5_GEN3_TIMER_SEL 0x51
  49. #define ADC5_GEN3_TIME_IMMEDIATE 0x1
  50. #define ADC5_GEN3_DIG_PARAM 0x52
  51. #define ADC5_GEN3_DIG_PARAM_CAL_SEL_MASK GENMASK(5, 4)
  52. #define ADC5_GEN3_DIG_PARAM_CAL_SEL_SHIFT 4
  53. #define ADC5_GEN3_DIG_PARAM_DEC_RATIO_SEL_MASK GENMASK(3, 2)
  54. #define ADC5_GEN3_DIG_PARAM_DEC_RATIO_SEL_SHIFT 2
  55. #define ADC5_GEN3_FAST_AVG 0x53
  56. #define ADC5_GEN3_FAST_AVG_CTL_EN BIT(7)
  57. #define ADC5_GEN3_FAST_AVG_CTL_SAMPLES_MASK GENMASK(2, 0)
  58. #define ADC5_GEN3_ADC_CH_SEL_CTL 0x54
  59. #define ADC5_GEN3_DELAY_CTL 0x55
  60. #define ADC5_GEN3_HW_SETTLE_DELAY_MASK 0xf
  61. #define ADC5_GEN3_CH_EN 0x56
  62. #define ADC5_GEN3_LOW_THR0 0x57
  63. #define ADC5_GEN3_LOW_THR1 0x58
  64. #define ADC5_GEN3_HIGH_THR0 0x59
  65. #define ADC5_GEN3_HIGH_THR1 0x5a
  66. #define ADC5_GEN3_CH0_DATA0 0x5c
  67. #define ADC5_GEN3_CH0_DATA1 0x5d
  68. #define ADC5_GEN3_CH1_DATA0 0x5e
  69. #define ADC5_GEN3_CH1_DATA1 0x5f
  70. #define ADC5_GEN3_CH2_DATA0 0x60
  71. #define ADC5_GEN3_CH2_DATA1 0x61
  72. #define ADC5_GEN3_CH3_DATA0 0x62
  73. #define ADC5_GEN3_CH3_DATA1 0x63
  74. #define ADC5_GEN3_CH4_DATA0 0x64
  75. #define ADC5_GEN3_CH4_DATA1 0x65
  76. #define ADC5_GEN3_CH5_DATA0 0x66
  77. #define ADC5_GEN3_CH5_DATA1 0x67
  78. #define ADC5_GEN3_CH6_DATA0 0x68
  79. #define ADC5_GEN3_CH6_DATA1 0x69
  80. #define ADC5_GEN3_CH7_DATA0 0x6a
  81. #define ADC5_GEN3_CH7_DATA1 0x6b
  82. #define ADC5_GEN3_CONV_REQ 0xe5
  83. #define ADC5_GEN3_CONV_REQ_REQ BIT(0)
  84. #define ADC5_GEN3_SID_OFFSET 0x8
  85. #define ADC5_GEN3_CHANNEL_MASK 0xff
  86. #define V_CHAN(x) (((x).sid << ADC5_GEN3_SID_OFFSET) | (x).channel)
  87. #define ADC_TM5_GEN3_LOWER_MASK(n) ((n) & GENMASK(7, 0))
  88. #define ADC_TM5_GEN3_UPPER_MASK(n) (((n) & GENMASK(15, 8)) >> 8)
  89. enum adc5_cal_method {
  90. ADC5_NO_CAL = 0,
  91. ADC5_RATIOMETRIC_CAL,
  92. ADC5_ABSOLUTE_CAL
  93. };
  94. enum adc_time_select {
  95. MEAS_INT_DISABLE = 0,
  96. MEAS_INT_IMMEDIATE,
  97. MEAS_INT_50MS,
  98. MEAS_INT_100MS,
  99. MEAS_INT_1S,
  100. MEAS_INT_NONE,
  101. };
  102. enum adc_tm_type {
  103. ADC_TM_NONE = 0,
  104. ADC_TM,
  105. ADC_TM_IIO,
  106. ADC_TM_NON_THERMAL,
  107. ADC_TM_INVALID,
  108. };
  109. static struct adc_tm_reverse_scale_fn adc_tm_rscale_fn[] = {
  110. [SCALE_R_ABSOLUTE] = {adc_tm_absolute_rthr_gen3},
  111. };
  112. struct adc5_base_data {
  113. u16 base_addr;
  114. const char *irq_name;
  115. int irq;
  116. };
  117. /**
  118. * struct adc5_channel_prop - ADC channel property.
  119. * @channel: channel number, refer to the channel list.
  120. * @cal_method: calibration method.
  121. * @decimation: sampling rate supported for the channel.
  122. * @sid: slave id of PMIC owning the channel, for PMIC5 Gen2 and above.
  123. * @prescale: channel scaling performed on the input signal.
  124. * @hw_settle_time: the time between AMUX being configured and the
  125. * start of conversion.
  126. * @avg_samples: ability to provide single result from the ADC
  127. * that is an average of multiple measurements.
  128. * @sdam_index: Index for which SDAM this channel is on.
  129. * @scale_fn_type: Represents the scaling function to convert voltage
  130. * physical units desired by the client for the channel.
  131. * @datasheet_name: Channel name used in device tree.
  132. * @chip: pointer to top-level ADC device structure.
  133. * @adc_tm: indicates TM type if the channel is used for TM measurements.
  134. * @tm_chan_index: TM channel number used (ranging from 1-7).
  135. * @timer: time period of recurring TM measurement.
  136. * @tzd: pointer to thermal device corresponding to TM channel.
  137. * @high_thr_en: TM high threshold crossing detection enabled.
  138. * @low_thr_en: TM low threshold crossing detection enabled.
  139. * @high_thr_triggered: indicates if high TM threshold has been triggered.
  140. * @low_thr_triggered: indicates if low TM threshold has been triggered.
  141. * @high_thr_voltage: upper threshold voltage for TM.
  142. * @low_thr_voltage: lower threshold voltage for TM.
  143. * @last_temp: last temperature that caused threshold violation,
  144. * or a thermal TM channel.
  145. * @last_temp_set: indicates if last_temp is stored.
  146. * @req_wq: workqueue holding queued notification tasks for a non-thermal
  147. * TM channel.
  148. * @work: scheduled work for handling non-thermal TM client notification.
  149. * @thr_list: list of client thresholds configured for non-thermal TM channel.
  150. * @adc_rscale_fn: reverse scaling function to convert voltage to raw code
  151. * for non-thermal TM channels.
  152. */
  153. struct adc5_channel_prop {
  154. unsigned int channel;
  155. enum adc5_cal_method cal_method;
  156. unsigned int decimation;
  157. unsigned int sid;
  158. unsigned int prescale;
  159. unsigned int hw_settle_time;
  160. unsigned int avg_samples;
  161. unsigned int sdam_index;
  162. enum vadc_scale_fn_type scale_fn_type;
  163. const char *datasheet_name;
  164. struct adc5_chip *chip;
  165. /* TM properties */
  166. int adc_tm;
  167. unsigned int tm_chan_index;
  168. unsigned int timer;
  169. struct thermal_zone_device *tzd;
  170. int high_thr_en;
  171. int low_thr_en;
  172. bool high_thr_triggered;
  173. bool low_thr_triggered;
  174. int64_t high_thr_voltage;
  175. int64_t low_thr_voltage;
  176. int last_temp;
  177. bool last_temp_set;
  178. struct workqueue_struct *req_wq;
  179. struct work_struct work;
  180. struct list_head thr_list;
  181. enum adc_tm_rscale_fn_type adc_rscale_fn;
  182. };
  183. /**
  184. * struct adc5_chip - ADC private structure.
  185. * @regmap: SPMI ADC5 peripheral register map field.
  186. * @dev: SPMI ADC5 device.
  187. * @base: pointer to array of ADC peripheral base and interrupt.
  188. * @debug_base: base address for the reserved ADC peripheral,
  189. * to dump for debug purposes alone.
  190. * @num_sdams: number of SDAMs being used.
  191. * @nchannels: number of ADC channels.
  192. * @chan_props: array of ADC channel properties.
  193. * @iio_chans: array of IIO channels specification.
  194. * @complete: ADC result notification after interrupt is received.
  195. * @lock: ADC lock for access to the peripheral.
  196. * @data: software configuration data.
  197. * @n_tm_channels: number of ADC channels used for TM measurements.
  198. * @list: list item, used to add this device to gloal list of ADC_TM devices.
  199. * @device_list: pointer to list of ADC_TM devices.
  200. * @tm_handler_work: scheduled work for handling TM threshold violation.
  201. */
  202. struct adc5_chip {
  203. struct regmap *regmap;
  204. struct device *dev;
  205. struct adc5_base_data *base;
  206. u16 debug_base;
  207. unsigned int num_sdams;
  208. unsigned int nchannels;
  209. struct adc5_channel_prop *chan_props;
  210. struct iio_chan_spec *iio_chans;
  211. struct completion complete;
  212. struct mutex lock;
  213. const struct adc5_data *data;
  214. /* TM properties */
  215. unsigned int n_tm_channels;
  216. struct list_head list;
  217. struct list_head *device_list;
  218. struct work_struct tm_handler_work;
  219. struct minidump_data *adc_md;
  220. };
  221. static int adc5_read(struct adc5_chip *adc, unsigned int sdam_index, u16 offset, u8 *data, int len)
  222. {
  223. int ret;
  224. ret = regmap_bulk_read(adc->regmap, adc->base[sdam_index].base_addr + offset, data, len);
  225. if (ret < 0)
  226. pr_err("adc read to register 0x%x of length:%d failed, ret=%d\n", offset, len, ret);
  227. return ret;
  228. }
  229. static int adc5_write(struct adc5_chip *adc, unsigned int sdam_index, u16 offset, u8 *data, int len)
  230. {
  231. int ret;
  232. ret = regmap_bulk_write(adc->regmap, adc->base[sdam_index].base_addr + offset, data, len);
  233. if (ret < 0)
  234. pr_err("adc write to register 0x%x of length:%d failed, ret=%d\n", offset, len,
  235. ret);
  236. return ret;
  237. }
  238. static int adc5_hw_settle_time_from_dt(u32 value,
  239. const unsigned int *hw_settle)
  240. {
  241. unsigned int i;
  242. for (i = 0; i < VADC_HW_SETTLE_SAMPLES_MAX; i++) {
  243. if (value == hw_settle[i])
  244. return i;
  245. }
  246. return -ENOENT;
  247. }
  248. static int adc5_avg_samples_from_dt(u32 value)
  249. {
  250. if (!is_power_of_2(value) || value > ADC5_AVG_SAMPLES_MAX)
  251. return -EINVAL;
  252. return __ffs(value);
  253. }
  254. static int adc5_decimation_from_dt(u32 value,
  255. const unsigned int *decimation)
  256. {
  257. unsigned int i;
  258. for (i = 0; i < ADC5_DECIMATION_SAMPLES_MAX; i++) {
  259. if (value == decimation[i])
  260. return i;
  261. }
  262. return -ENOENT;
  263. }
  264. #if IS_ENABLED(CONFIG_QCOM_SPMI_ADC5_GEN3_DEBUG_LOGGING)
  265. #define NUM_BYTES 8
  266. #define REG_COUNT 32
  267. static void adc5_gen3_dump_register(struct regmap *regmap, unsigned int offset)
  268. {
  269. int i, rc;
  270. u8 buf[NUM_BYTES];
  271. for (i = 0; i < REG_COUNT; i++) {
  272. rc = regmap_bulk_read(regmap, offset, buf, sizeof(buf));
  273. if (rc < 0) {
  274. pr_err("debug register dump failed with rc=%d\n", rc);
  275. return;
  276. }
  277. pr_err("%#04x: %*ph\n", offset, sizeof(buf), buf);
  278. offset += NUM_BYTES;
  279. }
  280. pr_err("\n");
  281. }
  282. static void adc5_gen3_dump_regs_debug(struct adc5_chip *adc)
  283. {
  284. u32 i;
  285. for (i = 0; i < adc->num_sdams; i++) {
  286. pr_err("ADC SDAM%d DUMP\n", i);
  287. adc5_gen3_dump_register(adc->regmap, adc->base[i].base_addr);
  288. }
  289. if (adc->debug_base) {
  290. pr_err("ADC DEBUG BASE DUMP\n");
  291. adc5_gen3_dump_register(adc->regmap, adc->debug_base);
  292. }
  293. BUG_ON(1);
  294. }
  295. #else
  296. static inline void adc5_gen3_dump_regs_debug(struct adc5_chip *adc)
  297. {}
  298. #endif
  299. static int adc5_gen3_read_voltage_data(struct adc5_chip *adc, u16 *data,
  300. u8 sdam_index)
  301. {
  302. int ret;
  303. u8 rslt[2];
  304. ret = adc5_read(adc, sdam_index, ADC5_GEN3_CH0_DATA0, rslt, 2);
  305. if (ret < 0)
  306. return ret;
  307. *data = (rslt[1] << 8) | rslt[0];
  308. if (*data == ADC5_USR_DATA_CHECK) {
  309. pr_err("Invalid data:%#x\n", *data);
  310. return -EINVAL;
  311. }
  312. pr_debug("voltage raw code:0x%x\n", *data);
  313. return 0;
  314. }
  315. static void adc5_gen3_update_dig_param(struct adc5_chip *adc,
  316. struct adc5_channel_prop *prop, u8 *data)
  317. {
  318. /* Update calibration select */
  319. *data &= ~ADC5_GEN3_DIG_PARAM_CAL_SEL_MASK;
  320. *data |= (prop->cal_method << ADC5_GEN3_DIG_PARAM_CAL_SEL_SHIFT);
  321. /* Update decimation ratio select */
  322. *data &= ~ADC5_GEN3_DIG_PARAM_DEC_RATIO_SEL_MASK;
  323. *data |= (prop->decimation << ADC5_GEN3_DIG_PARAM_DEC_RATIO_SEL_SHIFT);
  324. }
  325. static int adc5_gen3_configure(struct adc5_chip *adc,
  326. struct adc5_channel_prop *prop)
  327. {
  328. int ret;
  329. u8 conv_req = 0, buf[7];
  330. u8 sdam_index = prop->sdam_index;
  331. /* Reserve channel 0 of first SDAM for immediate conversions */
  332. if (prop->adc_tm)
  333. sdam_index = 0;
  334. ret = adc5_read(adc, sdam_index, ADC5_GEN3_SID, buf, sizeof(buf));
  335. if (ret < 0)
  336. return ret;
  337. /* Write SID */
  338. buf[0] = prop->sid & ADC5_GEN3_SID_MASK;
  339. /*
  340. * Use channel 0 by default for immediate conversion and
  341. * to indicate there is an actual conversion request
  342. */
  343. buf[1] = ADC5_GEN3_CHAN_CONV_REQ | 0;
  344. buf[2] = ADC5_GEN3_TIME_IMMEDIATE;
  345. /* Digital param selection */
  346. adc5_gen3_update_dig_param(adc, prop, &buf[3]);
  347. /* Update fast average sample value */
  348. buf[4] &= (u8) ~ADC5_GEN3_FAST_AVG_CTL_SAMPLES_MASK;
  349. buf[4] |= prop->avg_samples | ADC5_GEN3_FAST_AVG_CTL_EN;
  350. /* Select ADC channel */
  351. buf[5] = prop->channel;
  352. /* Select HW settle delay for channel */
  353. buf[6] &= (u8) ~ADC5_GEN3_HW_SETTLE_DELAY_MASK;
  354. buf[6] |= prop->hw_settle_time;
  355. reinit_completion(&adc->complete);
  356. ret = adc5_write(adc, sdam_index, ADC5_GEN3_SID, buf, sizeof(buf));
  357. if (ret < 0)
  358. return ret;
  359. conv_req = ADC5_GEN3_CONV_REQ_REQ;
  360. ret = adc5_write(adc, sdam_index, ADC5_GEN3_CONV_REQ, &conv_req, 1);
  361. return ret;
  362. }
  363. #define ADC5_GEN3_HS_DELAY_MIN_US 100
  364. #define ADC5_GEN3_HS_DELAY_MAX_US 110
  365. #define ADC5_GEN3_HS_RETRY_COUNT 150
  366. static int adc5_gen3_poll_wait_hs(struct adc5_chip *adc,
  367. unsigned int sdam_index)
  368. {
  369. int ret, count;
  370. u8 status = 0, conv_req = ADC5_GEN3_CONV_REQ_REQ;
  371. for (count = 0; count < ADC5_GEN3_HS_RETRY_COUNT; count++) {
  372. ret = adc5_read(adc, sdam_index, ADC5_GEN3_HS, &status, 1);
  373. if (ret < 0)
  374. return ret;
  375. if (status == ADC5_GEN3_HS_READY) {
  376. ret = adc5_read(adc, sdam_index, ADC5_GEN3_CONV_REQ,
  377. &conv_req, 1);
  378. if (ret < 0)
  379. return ret;
  380. if (!conv_req)
  381. break;
  382. }
  383. usleep_range(ADC5_GEN3_HS_DELAY_MIN_US,
  384. ADC5_GEN3_HS_DELAY_MAX_US);
  385. }
  386. if (count == ADC5_GEN3_HS_RETRY_COUNT) {
  387. pr_err("Setting HS ready bit timed out, status:%#x\n", status);
  388. return -ETIMEDOUT;
  389. }
  390. return 0;
  391. }
  392. #define ADC5_GEN3_CONV_TIMEOUT_MS 50
  393. #define ADC5_GEN3_POLL_ATTEMPTS 10
  394. static int adc5_gen3_do_conversion(struct adc5_chip *adc,
  395. struct adc5_channel_prop *prop,
  396. u16 *data_volt)
  397. {
  398. int ret, i;
  399. bool poll_eoc = false;
  400. unsigned long rc;
  401. unsigned int time_pending_ms;
  402. u8 val, eoc_status, sdam_index = prop->sdam_index;
  403. /* Reserve channel 0 of first SDAM for immediate conversions */
  404. if (prop->adc_tm)
  405. sdam_index = 0;
  406. mutex_lock(&adc->lock);
  407. ret = adc5_gen3_poll_wait_hs(adc, 0);
  408. if (ret < 0)
  409. goto unlock;
  410. ret = adc5_gen3_configure(adc, prop);
  411. if (ret < 0) {
  412. pr_err("ADC configure failed with %d\n", ret);
  413. goto unlock;
  414. }
  415. for (i = 0; i < ADC5_GEN3_POLL_ATTEMPTS; i++) {
  416. /* Trying both polling and waiting for interrupt */
  417. rc = wait_for_completion_timeout(&adc->complete,
  418. msecs_to_jiffies(ADC5_GEN3_CONV_TIMEOUT_MS));
  419. if (rc) {
  420. pr_debug("Got EOC interrupt after %d polling attempts\n", i);
  421. break;
  422. }
  423. /* CHAN0 is the preconfigured channel for immediate conversion */
  424. ret = adc5_read(adc, 0, ADC5_GEN3_EOC_STS, &eoc_status, 1);
  425. if (ret < 0) {
  426. pr_err("adc read eoc status failed with %d\n", ret);
  427. goto unlock;
  428. }
  429. if (eoc_status & ADC5_GEN3_EOC_CHAN_0) {
  430. poll_eoc = true;
  431. break;
  432. }
  433. }
  434. if (!rc && !poll_eoc) {
  435. pr_err("Reading ADC channel %s timed out\n",
  436. prop->datasheet_name);
  437. adc5_gen3_dump_regs_debug(adc);
  438. ret = -ETIMEDOUT;
  439. goto unlock;
  440. }
  441. time_pending_ms = jiffies_to_msecs(rc);
  442. pr_debug("ADC channel %s EOC took %u ms\n", prop->datasheet_name,
  443. (i + 1) * ADC5_GEN3_CONV_TIMEOUT_MS - time_pending_ms);
  444. ret = adc5_gen3_read_voltage_data(adc, data_volt, sdam_index);
  445. if (ret < 0)
  446. goto unlock;
  447. val = BIT(0);
  448. ret = adc5_write(adc, sdam_index, ADC5_GEN3_EOC_CLR, &val, 1);
  449. if (ret < 0)
  450. goto unlock;
  451. /* To indicate conversion request is only to clear a status */
  452. val = 0;
  453. ret = adc5_write(adc, sdam_index, ADC5_GEN3_PERPH_CH, &val, 1);
  454. if (ret < 0)
  455. goto unlock;
  456. val = ADC5_GEN3_CONV_REQ_REQ;
  457. ret = adc5_write(adc, sdam_index, ADC5_GEN3_CONV_REQ, &val, 1);
  458. unlock:
  459. mutex_unlock(&adc->lock);
  460. return ret;
  461. }
  462. static int get_sdam_from_irq(struct adc5_chip *adc, int irq)
  463. {
  464. int i;
  465. for (i = 0; i < adc->num_sdams; i++) {
  466. if (adc->base[i].irq == irq)
  467. return i;
  468. }
  469. return -ENOENT;
  470. }
  471. static irqreturn_t adc5_gen3_isr(int irq, void *dev_id)
  472. {
  473. struct adc5_chip *adc = dev_id;
  474. u8 status, tm_status[2], eoc_status, val;
  475. int ret, sdam_num;
  476. sdam_num = get_sdam_from_irq(adc, irq);
  477. if (sdam_num < 0) {
  478. pr_err("adc irq %d not associated with an sdam\n", irq);
  479. goto handler_end;
  480. }
  481. ret = adc5_read(adc, sdam_num, ADC5_GEN3_EOC_STS, &eoc_status, 1);
  482. if (ret < 0) {
  483. pr_err("adc read eoc status failed with %d\n", ret);
  484. goto handler_end;
  485. }
  486. /* CHAN0 is the preconfigured channel for immediate conversion */
  487. if (eoc_status & ADC5_GEN3_EOC_CHAN_0)
  488. complete(&adc->complete);
  489. ret = adc5_read(adc, sdam_num, ADC5_GEN3_TM_HIGH_STS, tm_status, 2);
  490. if (ret < 0) {
  491. pr_err("adc read TM status failed with %d\n", ret);
  492. goto handler_end;
  493. }
  494. if (tm_status[0] || tm_status[1])
  495. schedule_work(&adc->tm_handler_work);
  496. ret = adc5_read(adc, sdam_num, ADC5_GEN3_STATUS1, &status, 1);
  497. if (ret < 0) {
  498. pr_err("adc read status1 failed with %d\n", ret);
  499. goto handler_end;
  500. }
  501. pr_debug("Interrupt status:%#x, EOC status:%#x, high:%#x, low:%#x\n",
  502. status, eoc_status, tm_status[0], tm_status[1]);
  503. if (status & ADC5_GEN3_STATUS1_CONV_FAULT) {
  504. pr_err_ratelimited("Unexpected conversion fault, status:%#x, eoc_status:%#x\n",
  505. status, eoc_status);
  506. adc5_gen3_dump_regs_debug(adc);
  507. val = ADC5_GEN3_CONV_ERR_CLR_REQ;
  508. ret = adc5_write(adc, sdam_num, ADC5_GEN3_CONV_ERR_CLR, &val, 1);
  509. if (ret < 0)
  510. goto handler_end;
  511. /* To indicate conversion request is only to clear a status */
  512. val = 0;
  513. ret = adc5_write(adc, sdam_num, ADC5_GEN3_PERPH_CH, &val, 1);
  514. if (ret < 0)
  515. goto handler_end;
  516. val = ADC5_GEN3_CONV_REQ_REQ;
  517. ret = adc5_write(adc, sdam_num, ADC5_GEN3_CONV_REQ, &val, 1);
  518. if (ret < 0)
  519. goto handler_end;
  520. }
  521. return IRQ_HANDLED;
  522. handler_end:
  523. return IRQ_NONE;
  524. }
  525. static void tm_handler_work(struct work_struct *work)
  526. {
  527. struct adc5_channel_prop *chan_prop;
  528. u8 tm_status[2] = {0};
  529. u8 buf[16] = {0};
  530. u8 val;
  531. int ret, i, sdam_index = -1;
  532. struct adc5_chip *adc = container_of(work, struct adc5_chip,
  533. tm_handler_work);
  534. for (i = 0; i < adc->nchannels; i++) {
  535. bool upper_set = false, lower_set = false;
  536. u8 data_low = 0, data_high = 0;
  537. u16 code = 0;
  538. int temp, offset;
  539. chan_prop = &adc->chan_props[i];
  540. offset = chan_prop->tm_chan_index;
  541. if (chan_prop->adc_tm != ADC_TM && chan_prop->adc_tm != ADC_TM_NON_THERMAL)
  542. continue;
  543. mutex_lock(&adc->lock);
  544. if (chan_prop->sdam_index != sdam_index) {
  545. sdam_index = chan_prop->sdam_index;
  546. ret = adc5_read(adc, sdam_index, ADC5_GEN3_TM_HIGH_STS, tm_status, 2);
  547. if (ret < 0) {
  548. pr_err("adc read TM status failed with %d\n", ret);
  549. goto work_unlock;
  550. }
  551. ret = adc5_write(adc, sdam_index, ADC5_GEN3_TM_HIGH_STS_CLR, tm_status, 2);
  552. if (ret < 0) {
  553. pr_err("adc write TM status failed with %d\n", ret);
  554. goto work_unlock;
  555. }
  556. /* To indicate conversion request is only to clear a status */
  557. val = 0;
  558. ret = adc5_write(adc, sdam_index, ADC5_GEN3_PERPH_CH, &val, 1);
  559. if (ret < 0) {
  560. pr_err("adc write status clear conv_req failed with %d\n", ret);
  561. goto work_unlock;
  562. }
  563. val = ADC5_GEN3_CONV_REQ_REQ;
  564. ret = adc5_write(adc, sdam_index, ADC5_GEN3_CONV_REQ, &val, 1);
  565. if (ret < 0) {
  566. pr_err("adc write conv_req failed with %d\n", ret);
  567. goto work_unlock;
  568. }
  569. ret = adc5_read(adc, sdam_index, ADC5_GEN3_CH0_DATA0, buf, sizeof(buf));
  570. if (ret < 0) {
  571. pr_err("adc read data failed with %d\n", ret);
  572. goto work_unlock;
  573. }
  574. }
  575. if ((tm_status[0] & BIT(offset)) && (chan_prop->high_thr_en))
  576. upper_set = true;
  577. if ((tm_status[1] & BIT(offset)) && (chan_prop->low_thr_en))
  578. lower_set = true;
  579. mutex_unlock(&adc->lock);
  580. if (!(upper_set || lower_set))
  581. continue;
  582. data_low = buf[2 * offset];
  583. data_high = buf[2 * offset + 1];
  584. code = ((data_high << 8) | data_low);
  585. pr_debug("ADC_TM threshold code:0x%x\n", code);
  586. if (chan_prop->adc_tm == ADC_TM_NON_THERMAL) {
  587. if (lower_set) {
  588. mutex_lock(&adc->lock);
  589. chan_prop->low_thr_en = 0;
  590. chan_prop->low_thr_triggered = true;
  591. mutex_unlock(&adc->lock);
  592. queue_work(chan_prop->req_wq,
  593. &chan_prop->work);
  594. }
  595. if (upper_set) {
  596. mutex_lock(&adc->lock);
  597. chan_prop->high_thr_en = 0;
  598. chan_prop->high_thr_triggered = true;
  599. mutex_unlock(&adc->lock);
  600. queue_work(chan_prop->req_wq,
  601. &chan_prop->work);
  602. }
  603. } else {
  604. ret = qcom_adc5_hw_scale(chan_prop->scale_fn_type,
  605. chan_prop->prescale, adc->data, code, &temp);
  606. if (ret < 0) {
  607. pr_err("Invalid temperature reading, ret=%d, code=0x%x\n",
  608. ret, code);
  609. continue;
  610. }
  611. pr_debug("notifying thermal, temp:%d\n", temp);
  612. chan_prop->last_temp = temp;
  613. chan_prop->last_temp_set = true;
  614. thermal_zone_device_update(chan_prop->tzd, THERMAL_TRIP_VIOLATED);
  615. }
  616. }
  617. return;
  618. work_unlock:
  619. mutex_unlock(&adc->lock);
  620. }
  621. static int adc5_gen3_fwnode_xlate(struct iio_dev *indio_dev,
  622. const struct fwnode_reference_args *iiospec)
  623. {
  624. struct adc5_chip *adc = iio_priv(indio_dev);
  625. int i, v_channel;
  626. for (i = 0; i < adc->nchannels; i++) {
  627. v_channel = V_CHAN(adc->chan_props[i]);
  628. if (v_channel == iiospec->args[0])
  629. return i;
  630. }
  631. return -ENOENT;
  632. }
  633. static int adc5_gen3_read_raw(struct iio_dev *indio_dev,
  634. struct iio_chan_spec const *chan, int *val, int *val2,
  635. long mask)
  636. {
  637. struct adc5_chip *adc = iio_priv(indio_dev);
  638. struct adc5_channel_prop *prop;
  639. u16 adc_code_volt;
  640. int ret;
  641. prop = &adc->chan_props[chan->address];
  642. switch (mask) {
  643. case IIO_CHAN_INFO_PROCESSED:
  644. ret = adc5_gen3_do_conversion(adc, prop,
  645. &adc_code_volt);
  646. if (ret < 0)
  647. return ret;
  648. ret = qcom_adc5_hw_scale(prop->scale_fn_type,
  649. prop->prescale, adc->data,
  650. adc_code_volt, val);
  651. if (ret < 0)
  652. return ret;
  653. return IIO_VAL_INT;
  654. case IIO_CHAN_INFO_RAW:
  655. ret = adc5_gen3_do_conversion(adc, prop,
  656. &adc_code_volt);
  657. if (ret < 0)
  658. return ret;
  659. *val = (int)adc_code_volt;
  660. return IIO_VAL_INT;
  661. default:
  662. return -EINVAL;
  663. }
  664. return 0;
  665. }
  666. static const struct iio_info adc5_gen3_info = {
  667. .read_raw = adc5_gen3_read_raw,
  668. .fwnode_xlate = adc5_gen3_fwnode_xlate,
  669. };
  670. /* Used by thermal clients to read ADC channel temperature */
  671. int adc_tm_gen3_get_temp(struct thermal_zone_device *tz, int *temp)
  672. {
  673. int ret;
  674. struct adc5_channel_prop *prop = tz->devdata;
  675. struct adc5_chip *adc;
  676. u16 adc_code_volt;
  677. if (!prop || !prop->chip)
  678. return -EINVAL;
  679. adc = prop->chip;
  680. if (prop->last_temp_set) {
  681. pr_debug("last_temp: %d\n", prop->last_temp);
  682. prop->last_temp_set = false;
  683. *temp = prop->last_temp;
  684. return 0;
  685. }
  686. ret = adc5_gen3_do_conversion(adc, prop, &adc_code_volt);
  687. if (ret < 0)
  688. return ret;
  689. ret = qcom_adc5_hw_scale(prop->scale_fn_type,
  690. prop->prescale, adc->data,
  691. adc_code_volt, temp);
  692. /* Save temperature data to minidump */
  693. if (prop->chip->adc_md && prop->tzd)
  694. thermal_minidump_update_data(prop->chip->adc_md,
  695. prop->tzd->type, temp);
  696. return ret;
  697. }
  698. static int adc_tm5_gen3_configure(struct adc5_channel_prop *prop)
  699. {
  700. int ret;
  701. u8 conv_req = 0, buf[12];
  702. u32 mask = 0;
  703. struct adc5_chip *adc = prop->chip;
  704. ret = adc5_gen3_poll_wait_hs(adc, prop->sdam_index);
  705. if (ret < 0)
  706. return ret;
  707. ret = adc5_read(adc, prop->sdam_index, ADC5_GEN3_SID, buf, sizeof(buf));
  708. if (ret < 0)
  709. return ret;
  710. /* Write SID */
  711. buf[0] = prop->sid & ADC5_GEN3_SID_MASK;
  712. /*
  713. * Select TM channel and indicate there is an actual
  714. * conversion request
  715. */
  716. buf[1] = ADC5_GEN3_CHAN_CONV_REQ | prop->tm_chan_index;
  717. buf[2] = prop->timer;
  718. /* Digital param selection */
  719. adc5_gen3_update_dig_param(adc, prop, &buf[3]);
  720. /* Update fast average sample value */
  721. buf[4] &= (u8) ~ADC5_GEN3_FAST_AVG_CTL_SAMPLES_MASK;
  722. buf[4] |= prop->avg_samples | ADC5_GEN3_FAST_AVG_CTL_EN;
  723. /* Select ADC channel */
  724. buf[5] = prop->channel;
  725. /* Select HW settle delay for channel */
  726. buf[6] &= (u8) ~ADC5_GEN3_HW_SETTLE_DELAY_MASK;
  727. buf[6] |= prop->hw_settle_time;
  728. buf[7] = prop->high_thr_en << 1 | prop->low_thr_en;
  729. mask = lower_32_bits(prop->low_thr_voltage);
  730. buf[8] = ADC_TM5_GEN3_LOWER_MASK(mask);
  731. buf[9] = ADC_TM5_GEN3_UPPER_MASK(mask);
  732. mask = lower_32_bits(prop->high_thr_voltage);
  733. buf[10] = ADC_TM5_GEN3_LOWER_MASK(mask);
  734. buf[11] = ADC_TM5_GEN3_UPPER_MASK(mask);
  735. ret = adc5_write(adc, prop->sdam_index, ADC5_GEN3_SID, buf, sizeof(buf));
  736. if (ret < 0)
  737. return ret;
  738. conv_req = ADC5_GEN3_CONV_REQ_REQ;
  739. return adc5_write(adc, prop->sdam_index, ADC5_GEN3_CONV_REQ, &conv_req, 1);
  740. }
  741. static int adc_tm5_gen3_set_trip_temp(struct thermal_zone_device *tz,
  742. int low_temp, int high_temp)
  743. {
  744. struct adc5_channel_prop *prop = tz->devdata;
  745. struct adc5_chip *adc;
  746. struct adc_tm_config tm_config;
  747. int ret;
  748. if (!prop)
  749. return -EINVAL;
  750. pr_debug("channel:%s :low_temp(mdegC):%d, high_temp(mdegC):%d\n",
  751. prop->datasheet_name, low_temp, high_temp);
  752. adc = prop->chip;
  753. tm_config.high_thr_temp = tm_config.low_thr_temp = 0;
  754. if (high_temp != INT_MAX)
  755. tm_config.high_thr_temp = high_temp;
  756. if (low_temp != INT_MIN)
  757. tm_config.low_thr_temp = low_temp;
  758. if ((high_temp == INT_MAX) && (low_temp == INT_MIN)) {
  759. pr_err("No trips to set\n");
  760. return -EINVAL;
  761. }
  762. pr_debug("requested a low temp- %d and high temp- %d\n",
  763. tm_config.low_thr_temp, tm_config.high_thr_temp);
  764. adc_tm_scale_therm_voltage_100k_gen3(&tm_config);
  765. /*
  766. * Thresholds are forward scaled to confirm their
  767. * temperatures will actually cause a violation, before
  768. * being written.
  769. */
  770. pr_debug("high_thr:0x%llx, low_thr:0x%llx\n",
  771. tm_config.high_thr_voltage, tm_config.low_thr_voltage);
  772. mutex_lock(&adc->lock);
  773. if (high_temp != INT_MAX) {
  774. prop->low_thr_voltage = tm_config.low_thr_voltage;
  775. prop->low_thr_en = 1;
  776. } else {
  777. prop->low_thr_en = 0;
  778. }
  779. if (low_temp > -INT_MAX) {
  780. prop->high_thr_voltage = tm_config.high_thr_voltage;
  781. prop->high_thr_en = 1;
  782. } else {
  783. prop->high_thr_en = 0;
  784. }
  785. ret = adc_tm5_gen3_configure(prop);
  786. if (ret < 0)
  787. pr_err("Error during adc-tm configure:%d\n", ret);
  788. mutex_unlock(&adc->lock);
  789. return ret;
  790. }
  791. #define MAX_PROP_NAME_LEN 32
  792. struct adc5_chip *get_adc_tm_gen3(struct device *dev, const char *name)
  793. {
  794. struct platform_device *pdev;
  795. struct adc5_chip *chip;
  796. struct device_node *node = NULL;
  797. char prop_name[MAX_PROP_NAME_LEN];
  798. scnprintf(prop_name, MAX_PROP_NAME_LEN, "qcom,%s-adc_tm", name);
  799. node = of_parse_phandle(dev->of_node, prop_name, 0);
  800. if (node == NULL)
  801. return ERR_PTR(-ENODEV);
  802. list_for_each_entry(chip, &adc_tm_device_list, list) {
  803. pdev = to_platform_device(chip->dev);
  804. if (pdev->dev.of_node == node) {
  805. of_node_put(node);
  806. return chip;
  807. }
  808. }
  809. of_node_put(node);
  810. return ERR_PTR(-EPROBE_DEFER);
  811. }
  812. EXPORT_SYMBOL(get_adc_tm_gen3);
  813. static int32_t adc_tm_add_to_list(struct adc5_chip *chip,
  814. uint32_t dt_index,
  815. struct adc_tm_param *param)
  816. {
  817. struct adc_tm_client_info *client_info = NULL;
  818. bool client_info_exists = false;
  819. list_for_each_entry(client_info,
  820. &chip->chan_props[dt_index].thr_list, list) {
  821. if (client_info->param->id == param->id) {
  822. client_info->low_thr_requested = param->low_thr;
  823. client_info->high_thr_requested = param->high_thr;
  824. client_info->state_request = param->state_request;
  825. client_info->notify_low_thr = false;
  826. client_info->notify_high_thr = false;
  827. client_info_exists = true;
  828. }
  829. }
  830. if (!client_info_exists) {
  831. client_info = devm_kzalloc(chip->dev,
  832. sizeof(struct adc_tm_client_info), GFP_KERNEL);
  833. if (!client_info)
  834. return -ENOMEM;
  835. client_info->param->id = (uintptr_t) client_info;
  836. client_info->low_thr_requested = param->low_thr;
  837. client_info->high_thr_requested = param->high_thr;
  838. client_info->state_request = param->state_request;
  839. list_add_tail(&client_info->list,
  840. &chip->chan_props[dt_index].thr_list);
  841. }
  842. return 0;
  843. }
  844. static int32_t adc_tm_gen3_manage_thresholds(struct adc5_channel_prop *prop)
  845. {
  846. int high_thr = INT_MAX, low_thr = INT_MIN;
  847. struct adc_tm_client_info *client_info = NULL;
  848. struct list_head *thr_list;
  849. uint32_t scale_type = 0;
  850. struct adc_tm_config tm_config;
  851. prop->high_thr_en = 0;
  852. prop->low_thr_en = 0;
  853. /*
  854. * Reset the high_thr_set and low_thr_set of all
  855. * clients since the thresholds will be recomputed.
  856. */
  857. list_for_each(thr_list, &prop->thr_list) {
  858. client_info = list_entry(thr_list,
  859. struct adc_tm_client_info, list);
  860. client_info->high_thr_set = false;
  861. client_info->low_thr_set = false;
  862. }
  863. /* Find the min of high_thr and max of low_thr */
  864. list_for_each(thr_list, &prop->thr_list) {
  865. client_info = list_entry(thr_list,
  866. struct adc_tm_client_info, list);
  867. if ((client_info->state_request == ADC_TM_HIGH_THR_ENABLE) ||
  868. (client_info->state_request ==
  869. ADC_TM_HIGH_LOW_THR_ENABLE))
  870. if (client_info->high_thr_requested < high_thr)
  871. high_thr = client_info->high_thr_requested;
  872. if ((client_info->state_request == ADC_TM_LOW_THR_ENABLE) ||
  873. (client_info->state_request ==
  874. ADC_TM_HIGH_LOW_THR_ENABLE))
  875. if (client_info->low_thr_requested > low_thr)
  876. low_thr = client_info->low_thr_requested;
  877. pr_debug("threshold compared is high:%d and low:%d\n",
  878. client_info->high_thr_requested,
  879. client_info->low_thr_requested);
  880. pr_debug("current threshold is high:%d and low:%d\n",
  881. high_thr, low_thr);
  882. }
  883. /* Check which of the high_thr and low_thr got set */
  884. list_for_each(thr_list, &prop->thr_list) {
  885. client_info = list_entry(thr_list,
  886. struct adc_tm_client_info, list);
  887. if ((client_info->state_request == ADC_TM_HIGH_THR_ENABLE) ||
  888. (client_info->state_request ==
  889. ADC_TM_HIGH_LOW_THR_ENABLE))
  890. if (high_thr == client_info->high_thr_requested) {
  891. prop->high_thr_en = 1;
  892. client_info->high_thr_set = true;
  893. }
  894. if ((client_info->state_request == ADC_TM_LOW_THR_ENABLE) ||
  895. (client_info->state_request ==
  896. ADC_TM_HIGH_LOW_THR_ENABLE))
  897. if (low_thr == client_info->low_thr_requested) {
  898. prop->low_thr_en = 1;
  899. client_info->low_thr_set = true;
  900. }
  901. }
  902. tm_config.high_thr_voltage = (int64_t)high_thr;
  903. tm_config.low_thr_voltage = (int64_t)low_thr;
  904. scale_type = prop->adc_rscale_fn;
  905. if (scale_type >= SCALE_RSCALE_NONE)
  906. return -EBADF;
  907. adc_tm_rscale_fn[scale_type].chan(&tm_config);
  908. prop->low_thr_voltage = tm_config.low_thr_voltage;
  909. prop->high_thr_voltage = tm_config.high_thr_voltage;
  910. pr_debug("threshold written is high:%d and low:%d\n",
  911. high_thr, low_thr);
  912. return 0;
  913. }
  914. /* Used to notify non-thermal clients of threshold crossing */
  915. void notify_adc_tm_fn(struct work_struct *work)
  916. {
  917. struct adc5_channel_prop *prop = container_of(work,
  918. struct adc5_channel_prop, work);
  919. struct adc_tm_client_info *client_info = NULL;
  920. struct adc5_chip *chip;
  921. struct list_head *thr_list;
  922. int ret;
  923. chip = prop->chip;
  924. mutex_lock(&chip->lock);
  925. if (prop->low_thr_triggered) {
  926. /* adjust thr, calling manage_thr */
  927. list_for_each(thr_list, &prop->thr_list) {
  928. client_info = list_entry(thr_list,
  929. struct adc_tm_client_info, list);
  930. if (client_info->low_thr_set) {
  931. client_info->low_thr_set = false;
  932. client_info->notify_low_thr = true;
  933. if (client_info->state_request ==
  934. ADC_TM_HIGH_LOW_THR_ENABLE)
  935. client_info->state_request =
  936. ADC_TM_HIGH_THR_ENABLE;
  937. else
  938. client_info->state_request =
  939. ADC_TM_LOW_THR_DISABLE;
  940. }
  941. }
  942. prop->low_thr_triggered = false;
  943. }
  944. if (prop->high_thr_triggered) {
  945. /* adjust thr, calling manage_thr */
  946. list_for_each(thr_list, &prop->thr_list) {
  947. client_info = list_entry(thr_list,
  948. struct adc_tm_client_info, list);
  949. if (client_info->high_thr_set) {
  950. client_info->high_thr_set = false;
  951. client_info->notify_high_thr = true;
  952. if (client_info->state_request ==
  953. ADC_TM_HIGH_LOW_THR_ENABLE)
  954. client_info->state_request =
  955. ADC_TM_LOW_THR_ENABLE;
  956. else
  957. client_info->state_request =
  958. ADC_TM_HIGH_THR_DISABLE;
  959. }
  960. }
  961. prop->high_thr_triggered = false;
  962. }
  963. ret = adc_tm_gen3_manage_thresholds(prop);
  964. if (ret < 0)
  965. pr_err("Error in reverse scaling:%d\n", ret);
  966. ret = adc_tm5_gen3_configure(prop);
  967. if (ret < 0)
  968. pr_err("Error during adc-tm configure:%d\n", ret);
  969. mutex_unlock(&chip->lock);
  970. list_for_each_entry(client_info, &prop->thr_list, list) {
  971. if (client_info->notify_low_thr &&
  972. client_info->param->threshold_notification != NULL) {
  973. pr_debug("notify kernel with low state for channel 0x%x\n",
  974. prop->channel);
  975. client_info->param->threshold_notification(
  976. ADC_TM_LOW_STATE,
  977. client_info->param->btm_ctx);
  978. client_info->notify_low_thr = false;
  979. }
  980. if (client_info->notify_high_thr &&
  981. client_info->param->threshold_notification != NULL) {
  982. pr_debug("notify kernel with high state for channel 0x%x\n",
  983. prop->channel);
  984. client_info->param->threshold_notification(
  985. ADC_TM_HIGH_STATE,
  986. client_info->param->btm_ctx);
  987. client_info->notify_high_thr = false;
  988. }
  989. }
  990. }
  991. /* Used by non-thermal clients to configure an ADC_TM channel */
  992. int32_t adc_tm_channel_measure_gen3(struct adc5_chip *chip,
  993. struct adc_tm_param *param)
  994. {
  995. int ret, i;
  996. uint32_t v_channel, dt_index = 0;
  997. bool chan_found = false;
  998. if (param == NULL)
  999. return -EINVAL;
  1000. if (param->threshold_notification == NULL) {
  1001. pr_debug("No notification for high/low temp\n");
  1002. return -EINVAL;
  1003. }
  1004. for (i = 0; i < chip->nchannels; i++) {
  1005. v_channel = V_CHAN(chip->chan_props[i]);
  1006. if (v_channel == param->channel) {
  1007. dt_index = i;
  1008. chan_found = true;
  1009. break;
  1010. }
  1011. }
  1012. if (!chan_found) {
  1013. pr_err("not a valid ADC_TM channel\n");
  1014. return -EINVAL;
  1015. }
  1016. mutex_lock(&chip->lock);
  1017. /* add channel client to channel list */
  1018. adc_tm_add_to_list(chip, dt_index, param);
  1019. /* set right thresholds for the sensor */
  1020. ret = adc_tm_gen3_manage_thresholds(&chip->chan_props[dt_index]);
  1021. if (ret < 0)
  1022. pr_err("Error in reverse scaling:%d\n", ret);
  1023. /* configure channel */
  1024. ret = adc_tm5_gen3_configure(&chip->chan_props[dt_index]);
  1025. if (ret < 0) {
  1026. pr_err("Error during adc-tm configure:%d\n", ret);
  1027. goto fail_unlock;
  1028. }
  1029. fail_unlock:
  1030. mutex_unlock(&chip->lock);
  1031. return ret;
  1032. }
  1033. EXPORT_SYMBOL(adc_tm_channel_measure_gen3);
  1034. /* Used by non-thermal clients to release an ADC_TM channel */
  1035. int32_t adc_tm_disable_chan_meas_gen3(struct adc5_chip *chip,
  1036. struct adc_tm_param *param)
  1037. {
  1038. int ret = 0, i;
  1039. uint32_t dt_index = 0, v_channel;
  1040. struct adc_tm_client_info *client_info = NULL;
  1041. if (param == NULL)
  1042. return -EINVAL;
  1043. for (i = 0; i < chip->nchannels; i++) {
  1044. v_channel = V_CHAN(chip->chan_props[i]);
  1045. if (v_channel == param->channel) {
  1046. dt_index = i;
  1047. break;
  1048. }
  1049. }
  1050. if (i == chip->nchannels) {
  1051. pr_err("not a valid ADC_TM channel\n");
  1052. return -EINVAL;
  1053. }
  1054. mutex_lock(&chip->lock);
  1055. list_for_each_entry(client_info,
  1056. &chip->chan_props[i].thr_list, list) {
  1057. if (client_info->param->id == param->id) {
  1058. client_info->state_request =
  1059. ADC_TM_HIGH_LOW_THR_DISABLE;
  1060. ret = adc_tm_gen3_manage_thresholds(&chip->chan_props[i]);
  1061. if (ret < 0) {
  1062. pr_err("Error in reverse scaling:%d\n",
  1063. ret);
  1064. goto fail;
  1065. }
  1066. ret = adc_tm5_gen3_configure(&chip->chan_props[i]);
  1067. if (ret < 0) {
  1068. pr_err("Error during adc-tm configure:%d\n",
  1069. ret);
  1070. goto fail;
  1071. }
  1072. }
  1073. }
  1074. fail:
  1075. mutex_unlock(&chip->lock);
  1076. return ret;
  1077. }
  1078. EXPORT_SYMBOL(adc_tm_disable_chan_meas_gen3);
  1079. static struct thermal_zone_device_ops adc_tm_ops = {
  1080. .get_temp = adc_tm_gen3_get_temp,
  1081. .set_trips = adc_tm5_gen3_set_trip_temp,
  1082. };
  1083. static struct thermal_zone_device_ops adc_tm_ops_iio = {
  1084. .get_temp = adc_tm_gen3_get_temp,
  1085. };
  1086. static int adc_tm_register_tzd(struct adc5_chip *adc)
  1087. {
  1088. unsigned int i, channel;
  1089. struct thermal_zone_device *tzd;
  1090. for (i = 0; i < adc->nchannels; i++) {
  1091. channel = V_CHAN(adc->chan_props[i]);
  1092. switch (adc->chan_props[i].adc_tm) {
  1093. case ADC_TM_NONE:
  1094. continue;
  1095. case ADC_TM:
  1096. tzd = devm_thermal_of_zone_register(
  1097. adc->dev, channel,
  1098. &adc->chan_props[i], &adc_tm_ops);
  1099. break;
  1100. case ADC_TM_IIO:
  1101. tzd = devm_thermal_of_zone_register(
  1102. adc->dev, channel,
  1103. &adc->chan_props[i], &adc_tm_ops_iio);
  1104. break;
  1105. case ADC_TM_NON_THERMAL:
  1106. tzd = NULL;
  1107. break;
  1108. default:
  1109. pr_err("Invalid ADC_TM type:%d for dt_ch:%d\n",
  1110. adc->chan_props[i].adc_tm, adc->chan_props[i].channel);
  1111. return -EINVAL;
  1112. }
  1113. if (IS_ERR(tzd)) {
  1114. pr_err("Error registering TZ zone:%ld for dt_ch:%d\n",
  1115. PTR_ERR(tzd), adc->chan_props[i].channel);
  1116. return PTR_ERR(tzd);
  1117. }
  1118. adc->chan_props[i].tzd = tzd;
  1119. }
  1120. return 0;
  1121. }
  1122. struct adc5_channels {
  1123. const char *datasheet_name;
  1124. unsigned int prescale_index;
  1125. enum iio_chan_type type;
  1126. long info_mask;
  1127. enum vadc_scale_fn_type scale_fn_type;
  1128. };
  1129. /* In these definitions, _pre refers to an index into adc5_prescale_ratios. */
  1130. #define ADC5_CHAN(_dname, _type, _mask, _pre, _scale) \
  1131. { \
  1132. .datasheet_name = _dname, \
  1133. .prescale_index = _pre, \
  1134. .type = _type, \
  1135. .info_mask = _mask, \
  1136. .scale_fn_type = _scale, \
  1137. }, \
  1138. #define ADC5_CHAN_TEMP(_dname, _pre, _scale) \
  1139. ADC5_CHAN(_dname, IIO_TEMP, \
  1140. BIT(IIO_CHAN_INFO_PROCESSED), \
  1141. _pre, _scale) \
  1142. #define ADC5_CHAN_VOLT(_dname, _pre, _scale) \
  1143. ADC5_CHAN(_dname, IIO_VOLTAGE, \
  1144. BIT(IIO_CHAN_INFO_PROCESSED), \
  1145. _pre, _scale) \
  1146. #define ADC5_CHAN_CUR(_dname, _pre, _scale) \
  1147. ADC5_CHAN(_dname, IIO_CURRENT, \
  1148. BIT(IIO_CHAN_INFO_PROCESSED), \
  1149. _pre, _scale) \
  1150. static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = {
  1151. [ADC5_GEN3_OFFSET_REF] = ADC5_CHAN_VOLT("ref_gnd", 0,
  1152. SCALE_HW_CALIB_DEFAULT)
  1153. [ADC5_GEN3_1P25VREF] = ADC5_CHAN_VOLT("vref_1p25", 0,
  1154. SCALE_HW_CALIB_DEFAULT)
  1155. [ADC5_GEN3_VPH_PWR] = ADC5_CHAN_VOLT("vph_pwr", 1,
  1156. SCALE_HW_CALIB_DEFAULT)
  1157. [ADC5_GEN3_VBAT_SNS_QBG] = ADC5_CHAN_VOLT("vbat_sns", 1,
  1158. SCALE_HW_CALIB_DEFAULT)
  1159. [ADC5_GEN3_AMUX3_THM] = ADC5_CHAN_TEMP("smb_temp", 9,
  1160. SCALE_HW_CALIB_PM7_SMB_TEMP)
  1161. [ADC5_GEN3_AMUX5_THM] = ADC5_CHAN_VOLT("pot_res", 0,
  1162. SCALE_HW_CALIB_DEFAULT)
  1163. [ADC5_GEN3_CHG_TEMP] = ADC5_CHAN_TEMP("chg_temp", 0,
  1164. SCALE_HW_CALIB_PM7_CHG_TEMP)
  1165. [ADC5_GEN3_USB_SNS_V_16] = ADC5_CHAN_TEMP("usb_sns_v_div_16", 8,
  1166. SCALE_HW_CALIB_DEFAULT)
  1167. [ADC5_GEN3_VIN_DIV16_MUX] = ADC5_CHAN_TEMP("vin_div_16", 8,
  1168. SCALE_HW_CALIB_DEFAULT)
  1169. [ADC5_GEN3_IIN_FB] = ADC5_CHAN_CUR("iin_fb", 10,
  1170. SCALE_HW_CALIB_CUR)
  1171. [ADC5_GEN3_ICHG_SMB] = ADC5_CHAN_CUR("ichg_smb", 13,
  1172. SCALE_HW_CALIB_CUR)
  1173. [ADC5_GEN3_IIN_SMB] = ADC5_CHAN_CUR("iin_smb", 12,
  1174. SCALE_HW_CALIB_CUR)
  1175. [ADC5_GEN3_ICHG_FB] = ADC5_CHAN_CUR("ichg_fb", 16,
  1176. SCALE_HW_CALIB_CUR_RAW)
  1177. [ADC5_GEN3_DIE_TEMP] = ADC5_CHAN_TEMP("die_temp", 0,
  1178. SCALE_HW_CALIB_PMIC_THERM_PM7)
  1179. [ADC5_GEN3_TEMP_ALARM_LITE] = ADC5_CHAN_TEMP("die_temp_lite", 0,
  1180. SCALE_HW_CALIB_PMIC_THERM_PM7)
  1181. [ADC5_GEN3_AMUX1_THM_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_pu2", 0,
  1182. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  1183. [ADC5_GEN3_AMUX2_THM_100K_PU] = ADC5_CHAN_TEMP("amux_thm2_pu2", 0,
  1184. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  1185. [ADC5_GEN3_AMUX3_THM_100K_PU] = ADC5_CHAN_TEMP("amux_thm3_pu2", 0,
  1186. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  1187. [ADC5_GEN3_AMUX4_THM_100K_PU] = ADC5_CHAN_TEMP("amux_thm4_pu2", 0,
  1188. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  1189. [ADC5_GEN3_AMUX5_THM_100K_PU] = ADC5_CHAN_TEMP("amux_thm5_pu2", 0,
  1190. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  1191. [ADC5_GEN3_AMUX6_THM_100K_PU] = ADC5_CHAN_TEMP("amux_thm6_pu2", 0,
  1192. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  1193. [ADC5_GEN3_AMUX1_GPIO_100K_PU] = ADC5_CHAN_TEMP("amux1_gpio_pu2", 0,
  1194. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  1195. [ADC5_GEN3_AMUX2_GPIO_100K_PU] = ADC5_CHAN_TEMP("amux2_gpio_pu2", 0,
  1196. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  1197. [ADC5_GEN3_AMUX3_GPIO_100K_PU] = ADC5_CHAN_TEMP("amux3_gpio_pu2", 0,
  1198. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  1199. [ADC5_GEN3_AMUX4_GPIO_100K_PU] = ADC5_CHAN_TEMP("amux4_gpio_pu2", 0,
  1200. SCALE_HW_CALIB_THERM_100K_PU_PM7)
  1201. };
  1202. static int adc5_get_dt_channel_data(struct adc5_chip *adc,
  1203. struct adc5_channel_prop *prop,
  1204. struct device_node *node,
  1205. const struct adc5_data *data)
  1206. {
  1207. const char *name = node->name, *channel_name;
  1208. u32 chan, value, varr[2];
  1209. u32 sid = 0;
  1210. int ret, val;
  1211. struct device *dev = adc->dev;
  1212. ret = of_property_read_u32(node, "reg", &chan);
  1213. if (ret < 0) {
  1214. dev_err(dev, "invalid channel number %s\n", name);
  1215. return ret;
  1216. }
  1217. /*
  1218. * Value read from "reg" is virtual channel number
  1219. * virtual channel number = (sid << 8 | channel number).
  1220. */
  1221. sid = (chan >> ADC5_GEN3_SID_OFFSET);
  1222. chan = (chan & ADC5_GEN3_CHANNEL_MASK);
  1223. if (chan > ADC5_OFFSET_EXT2 ||
  1224. !data->adc_chans[chan].datasheet_name) {
  1225. dev_err(dev, "%s invalid channel number %d\n", name, chan);
  1226. return -EINVAL;
  1227. }
  1228. /* the channel has DT description */
  1229. prop->channel = chan;
  1230. prop->sid = sid;
  1231. channel_name = of_get_property(node,
  1232. "label", NULL) ? : node->name;
  1233. if (!channel_name) {
  1234. pr_err("Invalid channel name\n");
  1235. return -EINVAL;
  1236. }
  1237. prop->datasheet_name = channel_name;
  1238. ret = of_property_read_u32(node, "qcom,decimation", &value);
  1239. if (!ret) {
  1240. ret = adc5_decimation_from_dt(value, data->decimation);
  1241. if (ret < 0) {
  1242. dev_err(dev, "%02x invalid decimation %d\n",
  1243. chan, value);
  1244. return ret;
  1245. }
  1246. prop->decimation = ret;
  1247. } else {
  1248. prop->decimation = ADC5_DECIMATION_DEFAULT;
  1249. }
  1250. ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2);
  1251. if (!ret) {
  1252. ret = qcom_adc5_prescaling_from_dt(varr[0], varr[1]);
  1253. if (ret < 0) {
  1254. dev_err(dev, "%02x invalid pre-scaling <%d %d>\n",
  1255. chan, varr[0], varr[1]);
  1256. return ret;
  1257. }
  1258. prop->prescale = ret;
  1259. } else {
  1260. prop->prescale =
  1261. adc->data->adc_chans[prop->channel].prescale_index;
  1262. }
  1263. ret = of_property_read_u32(node, "qcom,hw-settle-time", &value);
  1264. if (!ret) {
  1265. ret = adc5_hw_settle_time_from_dt(value,
  1266. data->hw_settle_1);
  1267. if (ret < 0) {
  1268. dev_err(dev, "%02x invalid hw-settle-time %d us\n",
  1269. chan, value);
  1270. return ret;
  1271. }
  1272. prop->hw_settle_time = ret;
  1273. } else {
  1274. prop->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
  1275. }
  1276. ret = of_property_read_u32(node, "qcom,avg-samples", &value);
  1277. if (!ret) {
  1278. ret = adc5_avg_samples_from_dt(value);
  1279. if (ret < 0) {
  1280. dev_err(dev, "%02x invalid avg-samples %d\n",
  1281. chan, value);
  1282. return ret;
  1283. }
  1284. prop->avg_samples = ret;
  1285. } else {
  1286. prop->avg_samples = VADC_DEF_AVG_SAMPLES;
  1287. }
  1288. prop->scale_fn_type = -EINVAL;
  1289. ret = of_property_read_u32(node, "qcom,scale-fn-type", &value);
  1290. if (!ret && value < SCALE_HW_CALIB_INVALID)
  1291. prop->scale_fn_type = value;
  1292. if (of_property_read_bool(node, "qcom,ratiometric"))
  1293. prop->cal_method = ADC5_RATIOMETRIC_CAL;
  1294. else
  1295. prop->cal_method = ADC5_ABSOLUTE_CAL;
  1296. prop->timer = MEAS_INT_IMMEDIATE;
  1297. prop->adc_tm = ADC_TM_NONE;
  1298. ret = of_property_read_u32(node, "qcom,adc-tm-type", &value);
  1299. if (!ret && value < ADC_TM_INVALID)
  1300. prop->adc_tm = value;
  1301. if (prop->adc_tm == ADC_TM_NON_THERMAL) {
  1302. ret = of_property_read_u32(node, "qcom,rscale-type",
  1303. &prop->adc_rscale_fn);
  1304. if (ret < 0)
  1305. prop->adc_rscale_fn = SCALE_RSCALE_NONE;
  1306. }
  1307. if (prop->adc_tm && prop->adc_tm != ADC_TM_IIO) {
  1308. adc->n_tm_channels++;
  1309. if (adc->n_tm_channels > ((adc->num_sdams * 8) - 1)) {
  1310. pr_err("Number of TM nodes %u greater than channels supported:%u\n",
  1311. adc->n_tm_channels, (adc->num_sdams * 8) - 1);
  1312. return -EINVAL;
  1313. }
  1314. val = adc->n_tm_channels / 8;
  1315. prop->sdam_index = val;
  1316. prop->tm_chan_index = adc->n_tm_channels - (8*val);
  1317. prop->timer = MEAS_INT_1S;
  1318. if (prop->adc_tm == ADC_TM_NON_THERMAL) {
  1319. prop->req_wq = alloc_workqueue(
  1320. "adc_tm_notify_wq", WQ_HIGHPRI, 0);
  1321. if (!prop->req_wq) {
  1322. pr_err("Requesting priority wq failed\n");
  1323. return -ENOMEM;
  1324. }
  1325. INIT_WORK(&prop->work, notify_adc_tm_fn);
  1326. }
  1327. INIT_LIST_HEAD(&prop->thr_list);
  1328. }
  1329. dev_dbg(dev, "%02x name %s\n", chan, name);
  1330. return 0;
  1331. }
  1332. static const struct adc5_data adc5_gen3_data_pmic = {
  1333. .name = "pm-adc5-gen3",
  1334. .full_scale_code_volt = 0x70e4,
  1335. .full_scale_code_cur = 0x2ee0,
  1336. .adc_chans = adc5_chans_pmic,
  1337. .decimation = (unsigned int [ADC5_DECIMATION_SAMPLES_MAX])
  1338. {85, 340, 1360},
  1339. .hw_settle_1 = (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX])
  1340. {15, 100, 200, 300, 400, 500, 600, 700,
  1341. 1000, 2000, 4000, 8000, 16000, 32000,
  1342. 64000, 128000},
  1343. };
  1344. static const struct of_device_id adc5_match_table[] = {
  1345. {
  1346. .compatible = "qcom,spmi-adc5-gen3",
  1347. .data = &adc5_gen3_data_pmic,
  1348. },
  1349. { }
  1350. };
  1351. MODULE_DEVICE_TABLE(of, adc5_match_table);
  1352. static int adc5_get_dt_data(struct adc5_chip *adc, struct device_node *node)
  1353. {
  1354. const struct adc5_channels *adc_chan;
  1355. struct iio_chan_spec *iio_chan;
  1356. struct adc5_channel_prop *chan_props;
  1357. struct device_node *child;
  1358. unsigned int index = 0;
  1359. const struct of_device_id *id;
  1360. const struct adc5_data *data;
  1361. int ret;
  1362. adc->nchannels = of_get_available_child_count(node);
  1363. if (!adc->nchannels)
  1364. return -EINVAL;
  1365. adc->iio_chans = devm_kcalloc(adc->dev, adc->nchannels,
  1366. sizeof(*adc->iio_chans), GFP_KERNEL);
  1367. if (!adc->iio_chans)
  1368. return -ENOMEM;
  1369. adc->chan_props = devm_kcalloc(adc->dev, adc->nchannels,
  1370. sizeof(*adc->chan_props), GFP_KERNEL);
  1371. if (!adc->chan_props)
  1372. return -ENOMEM;
  1373. chan_props = adc->chan_props;
  1374. adc->n_tm_channels = 0;
  1375. iio_chan = adc->iio_chans;
  1376. id = of_match_node(adc5_match_table, node);
  1377. if (id)
  1378. data = id->data;
  1379. else
  1380. data = &adc5_gen3_data_pmic;
  1381. adc->data = data;
  1382. for_each_available_child_of_node(node, child) {
  1383. ret = adc5_get_dt_channel_data(adc, chan_props, child, data);
  1384. if (ret < 0) {
  1385. of_node_put(child);
  1386. return ret;
  1387. }
  1388. chan_props->chip = adc;
  1389. if (chan_props->scale_fn_type == -EINVAL)
  1390. chan_props->scale_fn_type =
  1391. data->adc_chans[chan_props->channel].scale_fn_type;
  1392. adc_chan = &data->adc_chans[chan_props->channel];
  1393. iio_chan->channel = chan_props->channel;
  1394. iio_chan->datasheet_name = chan_props->datasheet_name;
  1395. iio_chan->extend_name = chan_props->datasheet_name;
  1396. iio_chan->info_mask_separate = adc_chan->info_mask;
  1397. iio_chan->type = adc_chan->type;
  1398. iio_chan->address = index;
  1399. iio_chan++;
  1400. chan_props++;
  1401. index++;
  1402. }
  1403. return 0;
  1404. }
  1405. static int adc5_gen3_probe(struct platform_device *pdev)
  1406. {
  1407. struct device_node *node = pdev->dev.of_node;
  1408. struct device *dev = &pdev->dev;
  1409. struct iio_dev *indio_dev;
  1410. struct adc5_chip *adc;
  1411. struct regmap *regmap;
  1412. int ret, i;
  1413. u32 reg;
  1414. char buf[20];
  1415. regmap = dev_get_regmap(dev->parent, NULL);
  1416. if (!regmap)
  1417. return -ENODEV;
  1418. indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
  1419. if (!indio_dev)
  1420. return -ENOMEM;
  1421. adc = iio_priv(indio_dev);
  1422. adc->regmap = regmap;
  1423. adc->dev = dev;
  1424. ret = of_property_count_u32_elems(node, "reg");
  1425. if (ret < 0)
  1426. return ret;
  1427. adc->num_sdams = ret;
  1428. adc->base = devm_kcalloc(adc->dev, adc->num_sdams, sizeof(*adc->base), GFP_KERNEL);
  1429. if (!adc->base)
  1430. return -ENOMEM;
  1431. for (i = 0; i < adc->num_sdams; i++) {
  1432. ret = of_property_read_u32_index(node, "reg", i, &reg);
  1433. if (ret < 0)
  1434. return ret;
  1435. adc->base[i].base_addr = reg;
  1436. scnprintf(buf, sizeof(buf), "adc-sdam%d", i);
  1437. ret = of_irq_get_byname(node, buf);
  1438. if (ret < 0) {
  1439. pr_err("Failed to get irq for ADC5 GEN3 SDAM%d, ret=%d\n", i, ret);
  1440. return ret;
  1441. }
  1442. adc->base[i].irq = ret;
  1443. adc->base[i].irq_name = devm_kstrdup(adc->dev, buf, GFP_KERNEL);
  1444. if (!adc->base[i].irq_name)
  1445. return -ENOMEM;
  1446. }
  1447. if (!of_property_read_u32(node, "qcom,debug-base", &reg))
  1448. adc->debug_base = reg;
  1449. platform_set_drvdata(pdev, adc);
  1450. indio_dev->info = &adc5_gen3_info;
  1451. init_completion(&adc->complete);
  1452. mutex_init(&adc->lock);
  1453. ret = adc5_get_dt_data(adc, node);
  1454. if (ret < 0) {
  1455. pr_err("adc get dt data failed\n");
  1456. goto fail;
  1457. }
  1458. for (i = 0; i < adc->num_sdams; i++) {
  1459. ret = request_irq(adc->base[i].irq, adc5_gen3_isr,
  1460. 0, adc->base[i].irq_name, adc);
  1461. if (ret < 0)
  1462. goto irq_fail;
  1463. }
  1464. ret = adc_tm_register_tzd(adc);
  1465. if (ret < 0)
  1466. goto irq_fail;
  1467. adc->adc_md = thermal_minidump_register("adc5_gen3");
  1468. if (adc->n_tm_channels)
  1469. INIT_WORK(&adc->tm_handler_work, tm_handler_work);
  1470. indio_dev->dev.parent = dev;
  1471. indio_dev->dev.of_node = node;
  1472. indio_dev->name = pdev->name;
  1473. indio_dev->modes = INDIO_DIRECT_MODE;
  1474. indio_dev->channels = adc->iio_chans;
  1475. indio_dev->num_channels = adc->nchannels;
  1476. list_add_tail(&adc->list, &adc_tm_device_list);
  1477. adc->device_list = &adc_tm_device_list;
  1478. ret = devm_iio_device_register(dev, indio_dev);
  1479. if (!ret)
  1480. return 0;
  1481. irq_fail:
  1482. for (i = 0; i < adc->num_sdams; i++)
  1483. free_irq(adc->base[i].irq, adc);
  1484. fail:
  1485. i = 0;
  1486. while (i < adc->nchannels) {
  1487. if (adc->chan_props[i].req_wq)
  1488. destroy_workqueue(adc->chan_props[i].req_wq);
  1489. i++;
  1490. }
  1491. return ret;
  1492. }
  1493. static int adc5_gen3_remove(struct platform_device *pdev)
  1494. {
  1495. struct adc5_chip *adc = platform_get_drvdata(pdev);
  1496. u8 data = 0;
  1497. int i, sdam_index;
  1498. if (adc->n_tm_channels)
  1499. cancel_work_sync(&adc->tm_handler_work);
  1500. for (i = 0; i < adc->num_sdams; i++)
  1501. free_irq(adc->base[i].irq, adc);
  1502. mutex_lock(&adc->lock);
  1503. for (i = 0; i < adc->nchannels; i++) {
  1504. if (adc->chan_props[i].req_wq)
  1505. destroy_workqueue(adc->chan_props[i].req_wq);
  1506. adc->chan_props[i].timer = MEAS_INT_DISABLE;
  1507. }
  1508. /* Disable all available channels */
  1509. for (i = 0; i < adc->num_sdams * 8; i++) {
  1510. sdam_index = i / 8;
  1511. adc5_gen3_poll_wait_hs(adc, sdam_index);
  1512. data = MEAS_INT_DISABLE;
  1513. adc5_write(adc, sdam_index, ADC5_GEN3_TIMER_SEL, &data, 1);
  1514. /* To indicate there is an actual conversion request */
  1515. data = ADC5_GEN3_CHAN_CONV_REQ | (i - (sdam_index*8));
  1516. adc5_write(adc, sdam_index, ADC5_GEN3_PERPH_CH, &data, 1);
  1517. data = ADC5_GEN3_CONV_REQ_REQ;
  1518. adc5_write(adc, sdam_index, ADC5_GEN3_CONV_REQ, &data, 1);
  1519. }
  1520. mutex_unlock(&adc->lock);
  1521. mutex_destroy(&adc->lock);
  1522. list_del(&adc->list);
  1523. thermal_minidump_unregister(adc->adc_md);
  1524. return 0;
  1525. }
  1526. static int adc5_gen3_freeze(struct device *dev)
  1527. {
  1528. struct adc5_chip *adc = dev_get_drvdata(dev);
  1529. int i = 0;
  1530. mutex_lock(&adc->lock);
  1531. for (i = 0; i < adc->num_sdams; i++)
  1532. free_irq(adc->base[i].irq, adc);
  1533. mutex_unlock(&adc->lock);
  1534. return 0;
  1535. }
  1536. static int adc5_gen3_restore(struct device *dev)
  1537. {
  1538. struct adc5_chip *adc = dev_get_drvdata(dev);
  1539. int i = 0;
  1540. int ret = 0;
  1541. for (i = 0; i < adc->num_sdams; i++) {
  1542. ret = request_irq(adc->base[i].irq, adc5_gen3_isr,
  1543. 0, adc->base[i].irq_name, adc);
  1544. if (ret < 0)
  1545. return ret;
  1546. }
  1547. return ret;
  1548. }
  1549. static const struct dev_pm_ops adc5_gen3_pm_ops = {
  1550. .freeze = adc5_gen3_freeze,
  1551. .restore = adc5_gen3_restore,
  1552. };
  1553. static struct platform_driver adc5_gen3_driver = {
  1554. .driver = {
  1555. .name = "qcom-spmi-adc5-gen3",
  1556. .of_match_table = adc5_match_table,
  1557. .pm = &adc5_gen3_pm_ops,
  1558. },
  1559. .probe = adc5_gen3_probe,
  1560. .remove = adc5_gen3_remove,
  1561. };
  1562. module_platform_driver(adc5_gen3_driver);
  1563. MODULE_ALIAS("platform:qcom-spmi-adc5-gen3");
  1564. MODULE_DESCRIPTION("Qualcomm Technologies Inc. PMIC5 Gen3 ADC driver");
  1565. MODULE_LICENSE("GPL v2");