qcom-spmi-adc-tm5.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020 Linaro Limited
  4. *
  5. * Based on original driver:
  6. * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
  7. *
  8. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  9. */
  10. #include <linux/bitfield.h>
  11. #include <linux/iio/adc/qcom-vadc-common.h>
  12. #include <linux/iio/consumer.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/thermal.h>
  19. #include <linux/unaligned.h>
  20. #include "../thermal_hwmon.h"
  21. /*
  22. * Thermal monitoring block consists of 8 (ADC_TM5_NUM_CHANNELS) channels. Each
  23. * channel is programmed to use one of ADC channels for voltage comparison.
  24. * Voltages are programmed using ADC codes, so we have to convert temp to
  25. * voltage and then to ADC code value.
  26. *
  27. * Configuration of TM channels must match configuration of corresponding ADC
  28. * channels.
  29. */
  30. #define ADC5_MAX_CHANNEL 0xc0
  31. #define ADC_TM5_NUM_CHANNELS 8
  32. #define ADC_TM5_STATUS_LOW 0x0a
  33. #define ADC_TM5_STATUS_HIGH 0x0b
  34. #define ADC_TM5_NUM_BTM 0x0f
  35. #define ADC_TM5_ADC_DIG_PARAM 0x42
  36. #define ADC_TM5_FAST_AVG_CTL (ADC_TM5_ADC_DIG_PARAM + 1)
  37. #define ADC_TM5_FAST_AVG_EN BIT(7)
  38. #define ADC_TM5_MEAS_INTERVAL_CTL (ADC_TM5_ADC_DIG_PARAM + 2)
  39. #define ADC_TM5_TIMER1 3 /* 3.9ms */
  40. #define ADC_TM5_MEAS_INTERVAL_CTL2 (ADC_TM5_ADC_DIG_PARAM + 3)
  41. #define ADC_TM5_MEAS_INTERVAL_CTL2_MASK 0xf0
  42. #define ADC_TM5_TIMER2 10 /* 1 second */
  43. #define ADC_TM5_MEAS_INTERVAL_CTL3_MASK 0xf
  44. #define ADC_TM5_TIMER3 4 /* 4 second */
  45. #define ADC_TM_EN_CTL1 0x46
  46. #define ADC_TM_EN BIT(7)
  47. #define ADC_TM_CONV_REQ 0x47
  48. #define ADC_TM_CONV_REQ_EN BIT(7)
  49. #define ADC_TM5_M_CHAN_BASE 0x60
  50. #define ADC_TM5_M_ADC_CH_SEL_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 0)
  51. #define ADC_TM5_M_LOW_THR0(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 1)
  52. #define ADC_TM5_M_LOW_THR1(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 2)
  53. #define ADC_TM5_M_HIGH_THR0(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 3)
  54. #define ADC_TM5_M_HIGH_THR1(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 4)
  55. #define ADC_TM5_M_MEAS_INTERVAL_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 5)
  56. #define ADC_TM5_M_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 6)
  57. #define ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK 0xf
  58. #define ADC_TM5_M_CTL_CAL_SEL_MASK 0x30
  59. #define ADC_TM5_M_CTL_CAL_VAL 0x40
  60. #define ADC_TM5_M_EN(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 7)
  61. #define ADC_TM5_M_MEAS_EN BIT(7)
  62. #define ADC_TM5_M_HIGH_THR_INT_EN BIT(1)
  63. #define ADC_TM5_M_LOW_THR_INT_EN BIT(0)
  64. #define ADC_TM_GEN2_STATUS1 0x08
  65. #define ADC_TM_GEN2_STATUS_LOW_SET 0x09
  66. #define ADC_TM_GEN2_STATUS_LOW_CLR 0x0a
  67. #define ADC_TM_GEN2_STATUS_HIGH_SET 0x0b
  68. #define ADC_TM_GEN2_STATUS_HIGH_CLR 0x0c
  69. #define ADC_TM_GEN2_CFG_HS_SET 0x0d
  70. #define ADC_TM_GEN2_CFG_HS_FLAG BIT(0)
  71. #define ADC_TM_GEN2_CFG_HS_CLR 0x0e
  72. #define ADC_TM_GEN2_SID 0x40
  73. #define ADC_TM_GEN2_CH_CTL 0x41
  74. #define ADC_TM_GEN2_TM_CH_SEL GENMASK(7, 5)
  75. #define ADC_TM_GEN2_MEAS_INT_SEL GENMASK(3, 2)
  76. #define ADC_TM_GEN2_ADC_DIG_PARAM 0x42
  77. #define ADC_TM_GEN2_CTL_CAL_SEL GENMASK(5, 4)
  78. #define ADC_TM_GEN2_CTL_DEC_RATIO_MASK GENMASK(3, 2)
  79. #define ADC_TM_GEN2_FAST_AVG_CTL 0x43
  80. #define ADC_TM_GEN2_FAST_AVG_EN BIT(7)
  81. #define ADC_TM_GEN2_ADC_CH_SEL_CTL 0x44
  82. #define ADC_TM_GEN2_DELAY_CTL 0x45
  83. #define ADC_TM_GEN2_HW_SETTLE_DELAY GENMASK(3, 0)
  84. #define ADC_TM_GEN2_EN_CTL1 0x46
  85. #define ADC_TM_GEN2_EN BIT(7)
  86. #define ADC_TM_GEN2_CONV_REQ 0x47
  87. #define ADC_TM_GEN2_CONV_REQ_EN BIT(7)
  88. #define ADC_TM_GEN2_LOW_THR0 0x49
  89. #define ADC_TM_GEN2_LOW_THR1 0x4a
  90. #define ADC_TM_GEN2_HIGH_THR0 0x4b
  91. #define ADC_TM_GEN2_HIGH_THR1 0x4c
  92. #define ADC_TM_GEN2_LOWER_MASK(n) ((n) & GENMASK(7, 0))
  93. #define ADC_TM_GEN2_UPPER_MASK(n) (((n) & GENMASK(15, 8)) >> 8)
  94. #define ADC_TM_GEN2_MEAS_IRQ_EN 0x4d
  95. #define ADC_TM_GEN2_MEAS_EN BIT(7)
  96. #define ADC_TM5_GEN2_HIGH_THR_INT_EN BIT(1)
  97. #define ADC_TM5_GEN2_LOW_THR_INT_EN BIT(0)
  98. #define ADC_TM_GEN2_MEAS_INT_LSB 0x50
  99. #define ADC_TM_GEN2_MEAS_INT_MSB 0x51
  100. #define ADC_TM_GEN2_MEAS_INT_MODE 0x52
  101. #define ADC_TM_GEN2_Mn_DATA0(n) ((n * 2) + 0xa0)
  102. #define ADC_TM_GEN2_Mn_DATA1(n) ((n * 2) + 0xa1)
  103. #define ADC_TM_GEN2_DATA_SHIFT 8
  104. enum adc5_timer_select {
  105. ADC5_TIMER_SEL_1 = 0,
  106. ADC5_TIMER_SEL_2,
  107. ADC5_TIMER_SEL_3,
  108. ADC5_TIMER_SEL_NONE,
  109. };
  110. enum adc5_gen {
  111. ADC_TM5,
  112. ADC_TM_HC,
  113. ADC_TM5_GEN2,
  114. ADC_TM5_MAX
  115. };
  116. enum adc_tm5_cal_method {
  117. ADC_TM5_NO_CAL = 0,
  118. ADC_TM5_RATIOMETRIC_CAL,
  119. ADC_TM5_ABSOLUTE_CAL
  120. };
  121. enum adc_tm_gen2_time_select {
  122. MEAS_INT_50MS = 0,
  123. MEAS_INT_100MS,
  124. MEAS_INT_1S,
  125. MEAS_INT_SET,
  126. MEAS_INT_NONE,
  127. };
  128. struct adc_tm5_chip;
  129. struct adc_tm5_channel;
  130. struct adc_tm5_data {
  131. const u32 full_scale_code_volt;
  132. unsigned int *decimation;
  133. unsigned int *hw_settle;
  134. int (*disable_channel)(struct adc_tm5_channel *channel);
  135. int (*configure)(struct adc_tm5_channel *channel, int low, int high);
  136. irqreturn_t (*isr)(int irq, void *data);
  137. int (*init)(struct adc_tm5_chip *chip);
  138. char *irq_name;
  139. int gen;
  140. };
  141. /**
  142. * struct adc_tm5_channel - ADC Thermal Monitoring channel data.
  143. * @channel: channel number.
  144. * @adc_channel: corresponding ADC channel number.
  145. * @cal_method: calibration method.
  146. * @prescale: channel scaling performed on the input signal.
  147. * @hw_settle_time: the time between AMUX being configured and the
  148. * start of conversion.
  149. * @decimation: sampling rate supported for the channel.
  150. * @avg_samples: ability to provide single result from the ADC
  151. * that is an average of multiple measurements.
  152. * @high_thr_en: channel upper voltage threshold enable state.
  153. * @low_thr_en: channel lower voltage threshold enable state.
  154. * @meas_en: recurring measurement enable state
  155. * @iio: IIO channel instance used by this channel.
  156. * @chip: ADC TM chip instance.
  157. * @tzd: thermal zone device used by this channel.
  158. */
  159. struct adc_tm5_channel {
  160. unsigned int channel;
  161. unsigned int adc_channel;
  162. enum adc_tm5_cal_method cal_method;
  163. unsigned int prescale;
  164. unsigned int hw_settle_time;
  165. unsigned int decimation; /* For Gen2 ADC_TM */
  166. unsigned int avg_samples; /* For Gen2 ADC_TM */
  167. bool high_thr_en; /* For Gen2 ADC_TM */
  168. bool low_thr_en; /* For Gen2 ADC_TM */
  169. bool meas_en; /* For Gen2 ADC_TM */
  170. struct iio_channel *iio;
  171. struct adc_tm5_chip *chip;
  172. struct thermal_zone_device *tzd;
  173. };
  174. /**
  175. * struct adc_tm5_chip - ADC Thermal Monitoring properties
  176. * @regmap: SPMI ADC5 Thermal Monitoring peripheral register map field.
  177. * @dev: SPMI ADC5 device.
  178. * @data: software configuration data.
  179. * @channels: array of ADC TM channel data.
  180. * @nchannels: amount of channels defined/allocated
  181. * @decimation: sampling rate supported for the channel.
  182. * Applies to all channels, used only on Gen1 ADC_TM.
  183. * @avg_samples: ability to provide single result from the ADC
  184. * that is an average of multiple measurements. Applies to all
  185. * channels, used only on Gen1 ADC_TM.
  186. * @base: base address of TM registers.
  187. * @adc_mutex_lock: ADC_TM mutex lock, used only on Gen2 ADC_TM.
  188. * It is used to ensure only one ADC channel configuration
  189. * is done at a time using the shared set of configuration
  190. * registers.
  191. */
  192. struct adc_tm5_chip {
  193. struct regmap *regmap;
  194. struct device *dev;
  195. const struct adc_tm5_data *data;
  196. struct adc_tm5_channel *channels;
  197. unsigned int nchannels;
  198. unsigned int decimation;
  199. unsigned int avg_samples;
  200. u16 base;
  201. struct mutex adc_mutex_lock;
  202. };
  203. static int adc_tm5_read(struct adc_tm5_chip *adc_tm, u16 offset, u8 *data, int len)
  204. {
  205. return regmap_bulk_read(adc_tm->regmap, adc_tm->base + offset, data, len);
  206. }
  207. static int adc_tm5_write(struct adc_tm5_chip *adc_tm, u16 offset, u8 *data, int len)
  208. {
  209. return regmap_bulk_write(adc_tm->regmap, adc_tm->base + offset, data, len);
  210. }
  211. static int adc_tm5_reg_update(struct adc_tm5_chip *adc_tm, u16 offset, u8 mask, u8 val)
  212. {
  213. return regmap_write_bits(adc_tm->regmap, adc_tm->base + offset, mask, val);
  214. }
  215. static irqreturn_t adc_tm5_isr(int irq, void *data)
  216. {
  217. struct adc_tm5_chip *chip = data;
  218. u8 status_low, status_high, ctl;
  219. int ret, i;
  220. ret = adc_tm5_read(chip, ADC_TM5_STATUS_LOW, &status_low, sizeof(status_low));
  221. if (unlikely(ret)) {
  222. dev_err(chip->dev, "read status low failed: %d\n", ret);
  223. return IRQ_HANDLED;
  224. }
  225. ret = adc_tm5_read(chip, ADC_TM5_STATUS_HIGH, &status_high, sizeof(status_high));
  226. if (unlikely(ret)) {
  227. dev_err(chip->dev, "read status high failed: %d\n", ret);
  228. return IRQ_HANDLED;
  229. }
  230. for (i = 0; i < chip->nchannels; i++) {
  231. bool upper_set = false, lower_set = false;
  232. unsigned int ch = chip->channels[i].channel;
  233. /* No TZD, we warned at the boot time */
  234. if (!chip->channels[i].tzd)
  235. continue;
  236. ret = adc_tm5_read(chip, ADC_TM5_M_EN(ch), &ctl, sizeof(ctl));
  237. if (unlikely(ret)) {
  238. dev_err(chip->dev, "ctl read failed: %d, channel %d\n", ret, i);
  239. continue;
  240. }
  241. if (!(ctl & ADC_TM5_M_MEAS_EN))
  242. continue;
  243. lower_set = (status_low & BIT(ch)) &&
  244. (ctl & ADC_TM5_M_LOW_THR_INT_EN);
  245. upper_set = (status_high & BIT(ch)) &&
  246. (ctl & ADC_TM5_M_HIGH_THR_INT_EN);
  247. if (upper_set || lower_set)
  248. thermal_zone_device_update(chip->channels[i].tzd,
  249. THERMAL_EVENT_UNSPECIFIED);
  250. }
  251. return IRQ_HANDLED;
  252. }
  253. static irqreturn_t adc_tm5_gen2_isr(int irq, void *data)
  254. {
  255. struct adc_tm5_chip *chip = data;
  256. u8 status_low, status_high;
  257. int ret, i;
  258. ret = adc_tm5_read(chip, ADC_TM_GEN2_STATUS_LOW_CLR, &status_low, sizeof(status_low));
  259. if (ret) {
  260. dev_err(chip->dev, "read status_low failed: %d\n", ret);
  261. return IRQ_HANDLED;
  262. }
  263. ret = adc_tm5_read(chip, ADC_TM_GEN2_STATUS_HIGH_CLR, &status_high, sizeof(status_high));
  264. if (ret) {
  265. dev_err(chip->dev, "read status_high failed: %d\n", ret);
  266. return IRQ_HANDLED;
  267. }
  268. ret = adc_tm5_write(chip, ADC_TM_GEN2_STATUS_LOW_CLR, &status_low, sizeof(status_low));
  269. if (ret < 0) {
  270. dev_err(chip->dev, "clear status low failed with %d\n", ret);
  271. return IRQ_HANDLED;
  272. }
  273. ret = adc_tm5_write(chip, ADC_TM_GEN2_STATUS_HIGH_CLR, &status_high, sizeof(status_high));
  274. if (ret < 0) {
  275. dev_err(chip->dev, "clear status high failed with %d\n", ret);
  276. return IRQ_HANDLED;
  277. }
  278. for (i = 0; i < chip->nchannels; i++) {
  279. bool upper_set = false, lower_set = false;
  280. unsigned int ch = chip->channels[i].channel;
  281. /* No TZD, we warned at the boot time */
  282. if (!chip->channels[i].tzd)
  283. continue;
  284. if (!chip->channels[i].meas_en)
  285. continue;
  286. lower_set = (status_low & BIT(ch)) &&
  287. (chip->channels[i].low_thr_en);
  288. upper_set = (status_high & BIT(ch)) &&
  289. (chip->channels[i].high_thr_en);
  290. if (upper_set || lower_set)
  291. thermal_zone_device_update(chip->channels[i].tzd,
  292. THERMAL_EVENT_UNSPECIFIED);
  293. }
  294. return IRQ_HANDLED;
  295. }
  296. static int adc_tm5_get_temp(struct thermal_zone_device *tz, int *temp)
  297. {
  298. struct adc_tm5_channel *channel = thermal_zone_device_priv(tz);
  299. int ret;
  300. if (!channel || !channel->iio)
  301. return -EINVAL;
  302. ret = iio_read_channel_processed(channel->iio, temp);
  303. if (ret < 0)
  304. return ret;
  305. if (ret != IIO_VAL_INT)
  306. return -EINVAL;
  307. return 0;
  308. }
  309. static int adc_tm5_disable_channel(struct adc_tm5_channel *channel)
  310. {
  311. struct adc_tm5_chip *chip = channel->chip;
  312. unsigned int reg = ADC_TM5_M_EN(channel->channel);
  313. return adc_tm5_reg_update(chip, reg,
  314. ADC_TM5_M_MEAS_EN |
  315. ADC_TM5_M_HIGH_THR_INT_EN |
  316. ADC_TM5_M_LOW_THR_INT_EN,
  317. 0);
  318. }
  319. #define ADC_TM_GEN2_POLL_DELAY_MIN_US 100
  320. #define ADC_TM_GEN2_POLL_DELAY_MAX_US 110
  321. #define ADC_TM_GEN2_POLL_RETRY_COUNT 3
  322. static int32_t adc_tm5_gen2_conv_req(struct adc_tm5_chip *chip)
  323. {
  324. int ret;
  325. u8 data;
  326. unsigned int count;
  327. data = ADC_TM_GEN2_EN;
  328. ret = adc_tm5_write(chip, ADC_TM_GEN2_EN_CTL1, &data, 1);
  329. if (ret < 0) {
  330. dev_err(chip->dev, "adc-tm enable failed with %d\n", ret);
  331. return ret;
  332. }
  333. data = ADC_TM_GEN2_CFG_HS_FLAG;
  334. ret = adc_tm5_write(chip, ADC_TM_GEN2_CFG_HS_SET, &data, 1);
  335. if (ret < 0) {
  336. dev_err(chip->dev, "adc-tm handshake failed with %d\n", ret);
  337. return ret;
  338. }
  339. data = ADC_TM_GEN2_CONV_REQ_EN;
  340. ret = adc_tm5_write(chip, ADC_TM_GEN2_CONV_REQ, &data, 1);
  341. if (ret < 0) {
  342. dev_err(chip->dev, "adc-tm request conversion failed with %d\n", ret);
  343. return ret;
  344. }
  345. /*
  346. * SW sets a handshake bit and waits for PBS to clear it
  347. * before the next conversion request can be queued.
  348. */
  349. for (count = 0; count < ADC_TM_GEN2_POLL_RETRY_COUNT; count++) {
  350. ret = adc_tm5_read(chip, ADC_TM_GEN2_CFG_HS_SET, &data, sizeof(data));
  351. if (ret < 0) {
  352. dev_err(chip->dev, "adc-tm read failed with %d\n", ret);
  353. return ret;
  354. }
  355. if (!(data & ADC_TM_GEN2_CFG_HS_FLAG))
  356. return ret;
  357. usleep_range(ADC_TM_GEN2_POLL_DELAY_MIN_US,
  358. ADC_TM_GEN2_POLL_DELAY_MAX_US);
  359. }
  360. dev_err(chip->dev, "adc-tm conversion request handshake timed out\n");
  361. return -ETIMEDOUT;
  362. }
  363. static int adc_tm5_gen2_disable_channel(struct adc_tm5_channel *channel)
  364. {
  365. struct adc_tm5_chip *chip = channel->chip;
  366. int ret;
  367. u8 val;
  368. mutex_lock(&chip->adc_mutex_lock);
  369. channel->meas_en = false;
  370. channel->high_thr_en = false;
  371. channel->low_thr_en = false;
  372. ret = adc_tm5_read(chip, ADC_TM_GEN2_CH_CTL, &val, sizeof(val));
  373. if (ret < 0) {
  374. dev_err(chip->dev, "adc-tm block read failed with %d\n", ret);
  375. goto disable_fail;
  376. }
  377. val &= ~ADC_TM_GEN2_TM_CH_SEL;
  378. val |= FIELD_PREP(ADC_TM_GEN2_TM_CH_SEL, channel->channel);
  379. ret = adc_tm5_write(chip, ADC_TM_GEN2_CH_CTL, &val, 1);
  380. if (ret < 0) {
  381. dev_err(chip->dev, "adc-tm channel disable failed with %d\n", ret);
  382. goto disable_fail;
  383. }
  384. val = 0;
  385. ret = adc_tm5_write(chip, ADC_TM_GEN2_MEAS_IRQ_EN, &val, 1);
  386. if (ret < 0) {
  387. dev_err(chip->dev, "adc-tm interrupt disable failed with %d\n", ret);
  388. goto disable_fail;
  389. }
  390. ret = adc_tm5_gen2_conv_req(channel->chip);
  391. if (ret < 0)
  392. dev_err(chip->dev, "adc-tm channel configure failed with %d\n", ret);
  393. disable_fail:
  394. mutex_unlock(&chip->adc_mutex_lock);
  395. return ret;
  396. }
  397. static int adc_tm5_enable(struct adc_tm5_chip *chip)
  398. {
  399. int ret;
  400. u8 data;
  401. data = ADC_TM_EN;
  402. ret = adc_tm5_write(chip, ADC_TM_EN_CTL1, &data, sizeof(data));
  403. if (ret < 0) {
  404. dev_err(chip->dev, "adc-tm enable failed\n");
  405. return ret;
  406. }
  407. data = ADC_TM_CONV_REQ_EN;
  408. ret = adc_tm5_write(chip, ADC_TM_CONV_REQ, &data, sizeof(data));
  409. if (ret < 0) {
  410. dev_err(chip->dev, "adc-tm request conversion failed\n");
  411. return ret;
  412. }
  413. return 0;
  414. }
  415. static int adc_tm5_configure(struct adc_tm5_channel *channel, int low, int high)
  416. {
  417. struct adc_tm5_chip *chip = channel->chip;
  418. u8 buf[8];
  419. u16 reg = ADC_TM5_M_ADC_CH_SEL_CTL(channel->channel);
  420. int ret;
  421. ret = adc_tm5_read(chip, reg, buf, sizeof(buf));
  422. if (ret) {
  423. dev_err(chip->dev, "channel %d params read failed: %d\n", channel->channel, ret);
  424. return ret;
  425. }
  426. buf[0] = channel->adc_channel;
  427. /* High temperature corresponds to low voltage threshold */
  428. if (high != INT_MAX) {
  429. u16 adc_code = qcom_adc_tm5_temp_volt_scale(channel->prescale,
  430. chip->data->full_scale_code_volt, high);
  431. put_unaligned_le16(adc_code, &buf[1]);
  432. buf[7] |= ADC_TM5_M_LOW_THR_INT_EN;
  433. } else {
  434. buf[7] &= ~ADC_TM5_M_LOW_THR_INT_EN;
  435. }
  436. /* Low temperature corresponds to high voltage threshold */
  437. if (low != -INT_MAX) {
  438. u16 adc_code = qcom_adc_tm5_temp_volt_scale(channel->prescale,
  439. chip->data->full_scale_code_volt, low);
  440. put_unaligned_le16(adc_code, &buf[3]);
  441. buf[7] |= ADC_TM5_M_HIGH_THR_INT_EN;
  442. } else {
  443. buf[7] &= ~ADC_TM5_M_HIGH_THR_INT_EN;
  444. }
  445. buf[5] = ADC5_TIMER_SEL_2;
  446. /* Set calibration select, hw_settle delay */
  447. buf[6] &= ~ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK;
  448. buf[6] |= FIELD_PREP(ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK, channel->hw_settle_time);
  449. buf[6] &= ~ADC_TM5_M_CTL_CAL_SEL_MASK;
  450. buf[6] |= FIELD_PREP(ADC_TM5_M_CTL_CAL_SEL_MASK, channel->cal_method);
  451. buf[7] |= ADC_TM5_M_MEAS_EN;
  452. ret = adc_tm5_write(chip, reg, buf, sizeof(buf));
  453. if (ret) {
  454. dev_err(chip->dev, "channel %d params write failed: %d\n", channel->channel, ret);
  455. return ret;
  456. }
  457. return adc_tm5_enable(chip);
  458. }
  459. static int adc_tm5_gen2_configure(struct adc_tm5_channel *channel, int low, int high)
  460. {
  461. struct adc_tm5_chip *chip = channel->chip;
  462. int ret;
  463. u8 buf[14];
  464. u16 adc_code;
  465. mutex_lock(&chip->adc_mutex_lock);
  466. channel->meas_en = true;
  467. ret = adc_tm5_read(chip, ADC_TM_GEN2_SID, buf, sizeof(buf));
  468. if (ret < 0) {
  469. dev_err(chip->dev, "adc-tm block read failed with %d\n", ret);
  470. goto config_fail;
  471. }
  472. /* Set SID from virtual channel number */
  473. buf[0] = channel->adc_channel >> 8;
  474. /* Set TM channel number used and measurement interval */
  475. buf[1] &= ~ADC_TM_GEN2_TM_CH_SEL;
  476. buf[1] |= FIELD_PREP(ADC_TM_GEN2_TM_CH_SEL, channel->channel);
  477. buf[1] &= ~ADC_TM_GEN2_MEAS_INT_SEL;
  478. buf[1] |= FIELD_PREP(ADC_TM_GEN2_MEAS_INT_SEL, MEAS_INT_1S);
  479. buf[2] &= ~ADC_TM_GEN2_CTL_DEC_RATIO_MASK;
  480. buf[2] |= FIELD_PREP(ADC_TM_GEN2_CTL_DEC_RATIO_MASK, channel->decimation);
  481. buf[2] &= ~ADC_TM_GEN2_CTL_CAL_SEL;
  482. buf[2] |= FIELD_PREP(ADC_TM_GEN2_CTL_CAL_SEL, channel->cal_method);
  483. buf[3] = channel->avg_samples | ADC_TM_GEN2_FAST_AVG_EN;
  484. buf[4] = channel->adc_channel & 0xff;
  485. buf[5] = channel->hw_settle_time & ADC_TM_GEN2_HW_SETTLE_DELAY;
  486. /* High temperature corresponds to low voltage threshold */
  487. if (high != INT_MAX) {
  488. channel->low_thr_en = true;
  489. adc_code = qcom_adc_tm5_gen2_temp_res_scale(high);
  490. put_unaligned_le16(adc_code, &buf[9]);
  491. } else {
  492. channel->low_thr_en = false;
  493. }
  494. /* Low temperature corresponds to high voltage threshold */
  495. if (low != -INT_MAX) {
  496. channel->high_thr_en = true;
  497. adc_code = qcom_adc_tm5_gen2_temp_res_scale(low);
  498. put_unaligned_le16(adc_code, &buf[11]);
  499. } else {
  500. channel->high_thr_en = false;
  501. }
  502. buf[13] = ADC_TM_GEN2_MEAS_EN;
  503. if (channel->high_thr_en)
  504. buf[13] |= ADC_TM5_GEN2_HIGH_THR_INT_EN;
  505. if (channel->low_thr_en)
  506. buf[13] |= ADC_TM5_GEN2_LOW_THR_INT_EN;
  507. ret = adc_tm5_write(chip, ADC_TM_GEN2_SID, buf, sizeof(buf));
  508. if (ret) {
  509. dev_err(chip->dev, "channel %d params write failed: %d\n", channel->channel, ret);
  510. goto config_fail;
  511. }
  512. ret = adc_tm5_gen2_conv_req(channel->chip);
  513. if (ret < 0)
  514. dev_err(chip->dev, "adc-tm channel configure failed with %d\n", ret);
  515. config_fail:
  516. mutex_unlock(&chip->adc_mutex_lock);
  517. return ret;
  518. }
  519. static int adc_tm5_set_trips(struct thermal_zone_device *tz, int low, int high)
  520. {
  521. struct adc_tm5_channel *channel = thermal_zone_device_priv(tz);
  522. struct adc_tm5_chip *chip;
  523. int ret;
  524. if (!channel)
  525. return -EINVAL;
  526. chip = channel->chip;
  527. dev_dbg(chip->dev, "%d:low(mdegC):%d, high(mdegC):%d\n",
  528. channel->channel, low, high);
  529. if (high == INT_MAX && low <= -INT_MAX)
  530. ret = chip->data->disable_channel(channel);
  531. else
  532. ret = chip->data->configure(channel, low, high);
  533. return ret;
  534. }
  535. static const struct thermal_zone_device_ops adc_tm5_thermal_ops = {
  536. .get_temp = adc_tm5_get_temp,
  537. .set_trips = adc_tm5_set_trips,
  538. };
  539. static int adc_tm5_register_tzd(struct adc_tm5_chip *adc_tm)
  540. {
  541. unsigned int i;
  542. struct thermal_zone_device *tzd;
  543. for (i = 0; i < adc_tm->nchannels; i++) {
  544. adc_tm->channels[i].chip = adc_tm;
  545. tzd = devm_thermal_of_zone_register(adc_tm->dev,
  546. adc_tm->channels[i].channel,
  547. &adc_tm->channels[i],
  548. &adc_tm5_thermal_ops);
  549. if (IS_ERR(tzd)) {
  550. if (PTR_ERR(tzd) == -ENODEV) {
  551. dev_dbg(adc_tm->dev, "thermal sensor on channel %d is not used\n",
  552. adc_tm->channels[i].channel);
  553. continue;
  554. }
  555. dev_err(adc_tm->dev, "Error registering TZ zone for channel %d: %ld\n",
  556. adc_tm->channels[i].channel, PTR_ERR(tzd));
  557. return PTR_ERR(tzd);
  558. }
  559. adc_tm->channels[i].tzd = tzd;
  560. devm_thermal_add_hwmon_sysfs(adc_tm->dev, tzd);
  561. }
  562. return 0;
  563. }
  564. static int adc_tm_hc_init(struct adc_tm5_chip *chip)
  565. {
  566. unsigned int i;
  567. u8 buf[2];
  568. int ret;
  569. for (i = 0; i < chip->nchannels; i++) {
  570. if (chip->channels[i].channel >= ADC_TM5_NUM_CHANNELS) {
  571. dev_err(chip->dev, "Invalid channel %d\n", chip->channels[i].channel);
  572. return -EINVAL;
  573. }
  574. }
  575. buf[0] = chip->decimation;
  576. buf[1] = chip->avg_samples | ADC_TM5_FAST_AVG_EN;
  577. ret = adc_tm5_write(chip, ADC_TM5_ADC_DIG_PARAM, buf, sizeof(buf));
  578. if (ret)
  579. dev_err(chip->dev, "block write failed: %d\n", ret);
  580. return ret;
  581. }
  582. static int adc_tm5_init(struct adc_tm5_chip *chip)
  583. {
  584. u8 buf[4], channels_available;
  585. int ret;
  586. unsigned int i;
  587. ret = adc_tm5_read(chip, ADC_TM5_NUM_BTM,
  588. &channels_available, sizeof(channels_available));
  589. if (ret) {
  590. dev_err(chip->dev, "read failed for BTM channels\n");
  591. return ret;
  592. }
  593. for (i = 0; i < chip->nchannels; i++) {
  594. if (chip->channels[i].channel >= channels_available) {
  595. dev_err(chip->dev, "Invalid channel %d\n", chip->channels[i].channel);
  596. return -EINVAL;
  597. }
  598. }
  599. buf[0] = chip->decimation;
  600. buf[1] = chip->avg_samples | ADC_TM5_FAST_AVG_EN;
  601. buf[2] = ADC_TM5_TIMER1;
  602. buf[3] = FIELD_PREP(ADC_TM5_MEAS_INTERVAL_CTL2_MASK, ADC_TM5_TIMER2) |
  603. FIELD_PREP(ADC_TM5_MEAS_INTERVAL_CTL3_MASK, ADC_TM5_TIMER3);
  604. ret = adc_tm5_write(chip, ADC_TM5_ADC_DIG_PARAM, buf, sizeof(buf));
  605. if (ret) {
  606. dev_err(chip->dev, "block write failed: %d\n", ret);
  607. return ret;
  608. }
  609. return ret;
  610. }
  611. static int adc_tm5_gen2_init(struct adc_tm5_chip *chip)
  612. {
  613. u8 channels_available;
  614. int ret;
  615. unsigned int i;
  616. ret = adc_tm5_read(chip, ADC_TM5_NUM_BTM,
  617. &channels_available, sizeof(channels_available));
  618. if (ret) {
  619. dev_err(chip->dev, "read failed for BTM channels\n");
  620. return ret;
  621. }
  622. for (i = 0; i < chip->nchannels; i++) {
  623. if (chip->channels[i].channel >= channels_available) {
  624. dev_err(chip->dev, "Invalid channel %d\n", chip->channels[i].channel);
  625. return -EINVAL;
  626. }
  627. }
  628. mutex_init(&chip->adc_mutex_lock);
  629. return ret;
  630. }
  631. static int adc_tm5_get_dt_channel_data(struct adc_tm5_chip *adc_tm,
  632. struct adc_tm5_channel *channel,
  633. struct device_node *node)
  634. {
  635. const char *name = node->name;
  636. u32 chan, value, adc_channel, varr[2];
  637. int ret;
  638. struct device *dev = adc_tm->dev;
  639. struct of_phandle_args args;
  640. ret = of_property_read_u32(node, "reg", &chan);
  641. if (ret) {
  642. dev_err(dev, "%s: invalid channel number %d\n", name, ret);
  643. return ret;
  644. }
  645. if (chan >= ADC_TM5_NUM_CHANNELS) {
  646. dev_err(dev, "%s: channel number too big: %d\n", name, chan);
  647. return -EINVAL;
  648. }
  649. channel->channel = chan;
  650. /*
  651. * We are tied to PMIC's ADC controller, which always use single
  652. * argument for channel number. So don't bother parsing
  653. * #io-channel-cells, just enforce cell_count = 1.
  654. */
  655. ret = of_parse_phandle_with_fixed_args(node, "io-channels", 1, 0, &args);
  656. if (ret < 0) {
  657. dev_err(dev, "%s: error parsing ADC channel number %d: %d\n", name, chan, ret);
  658. return ret;
  659. }
  660. of_node_put(args.np);
  661. if (args.args_count != 1) {
  662. dev_err(dev, "%s: invalid args count for ADC channel %d\n", name, chan);
  663. return -EINVAL;
  664. }
  665. adc_channel = args.args[0];
  666. if (adc_tm->data->gen == ADC_TM5_GEN2)
  667. adc_channel &= 0xff;
  668. if (adc_channel >= ADC5_MAX_CHANNEL) {
  669. dev_err(dev, "%s: invalid ADC channel number %d\n", name, chan);
  670. return -EINVAL;
  671. }
  672. channel->adc_channel = args.args[0];
  673. channel->iio = devm_fwnode_iio_channel_get_by_name(adc_tm->dev,
  674. of_fwnode_handle(node), NULL);
  675. if (IS_ERR(channel->iio))
  676. return dev_err_probe(dev, PTR_ERR(channel->iio), "%s: error getting channel\n",
  677. name);
  678. ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2);
  679. if (!ret) {
  680. ret = qcom_adc5_prescaling_from_dt(varr[0], varr[1]);
  681. if (ret < 0) {
  682. dev_err(dev, "%s: invalid pre-scaling <%d %d>\n",
  683. name, varr[0], varr[1]);
  684. return ret;
  685. }
  686. channel->prescale = ret;
  687. } else {
  688. /* 1:1 prescale is index 0 */
  689. channel->prescale = 0;
  690. }
  691. ret = of_property_read_u32(node, "qcom,hw-settle-time-us", &value);
  692. if (!ret) {
  693. ret = qcom_adc5_hw_settle_time_from_dt(value, adc_tm->data->hw_settle);
  694. if (ret < 0) {
  695. dev_err(dev, "%s invalid hw-settle-time-us %d us\n",
  696. name, value);
  697. return ret;
  698. }
  699. channel->hw_settle_time = ret;
  700. } else {
  701. channel->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
  702. }
  703. if (of_property_read_bool(node, "qcom,ratiometric"))
  704. channel->cal_method = ADC_TM5_RATIOMETRIC_CAL;
  705. else
  706. channel->cal_method = ADC_TM5_ABSOLUTE_CAL;
  707. if (adc_tm->data->gen == ADC_TM5_GEN2) {
  708. ret = of_property_read_u32(node, "qcom,decimation", &value);
  709. if (!ret) {
  710. ret = qcom_adc5_decimation_from_dt(value, adc_tm->data->decimation);
  711. if (ret < 0) {
  712. dev_err(dev, "invalid decimation %d\n", value);
  713. return ret;
  714. }
  715. channel->decimation = ret;
  716. } else {
  717. channel->decimation = ADC5_DECIMATION_DEFAULT;
  718. }
  719. ret = of_property_read_u32(node, "qcom,avg-samples", &value);
  720. if (!ret) {
  721. ret = qcom_adc5_avg_samples_from_dt(value);
  722. if (ret < 0) {
  723. dev_err(dev, "invalid avg-samples %d\n", value);
  724. return ret;
  725. }
  726. channel->avg_samples = ret;
  727. } else {
  728. channel->avg_samples = VADC_DEF_AVG_SAMPLES;
  729. }
  730. }
  731. return 0;
  732. }
  733. static const struct adc_tm5_data adc_tm5_data_pmic = {
  734. .full_scale_code_volt = 0x70e4,
  735. .decimation = (unsigned int []) { 250, 420, 840 },
  736. .hw_settle = (unsigned int []) { 15, 100, 200, 300, 400, 500, 600, 700,
  737. 1000, 2000, 4000, 8000, 16000, 32000,
  738. 64000, 128000 },
  739. .disable_channel = adc_tm5_disable_channel,
  740. .configure = adc_tm5_configure,
  741. .isr = adc_tm5_isr,
  742. .init = adc_tm5_init,
  743. .irq_name = "pm-adc-tm5",
  744. .gen = ADC_TM5,
  745. };
  746. static const struct adc_tm5_data adc_tm_hc_data_pmic = {
  747. .full_scale_code_volt = 0x70e4,
  748. .decimation = (unsigned int []) { 256, 512, 1024 },
  749. .hw_settle = (unsigned int []) { 0, 100, 200, 300, 400, 500, 600, 700,
  750. 1000, 2000, 4000, 6000, 8000, 10000 },
  751. .disable_channel = adc_tm5_disable_channel,
  752. .configure = adc_tm5_configure,
  753. .isr = adc_tm5_isr,
  754. .init = adc_tm_hc_init,
  755. .irq_name = "pm-adc-tm5",
  756. .gen = ADC_TM_HC,
  757. };
  758. static const struct adc_tm5_data adc_tm5_gen2_data_pmic = {
  759. .full_scale_code_volt = 0x70e4,
  760. .decimation = (unsigned int []) { 85, 340, 1360 },
  761. .hw_settle = (unsigned int []) { 15, 100, 200, 300, 400, 500, 600, 700,
  762. 1000, 2000, 4000, 8000, 16000, 32000,
  763. 64000, 128000 },
  764. .disable_channel = adc_tm5_gen2_disable_channel,
  765. .configure = adc_tm5_gen2_configure,
  766. .isr = adc_tm5_gen2_isr,
  767. .init = adc_tm5_gen2_init,
  768. .irq_name = "pm-adc-tm5-gen2",
  769. .gen = ADC_TM5_GEN2,
  770. };
  771. static int adc_tm5_get_dt_data(struct adc_tm5_chip *adc_tm, struct device_node *node)
  772. {
  773. struct adc_tm5_channel *channels;
  774. u32 value;
  775. int ret;
  776. struct device *dev = adc_tm->dev;
  777. adc_tm->nchannels = of_get_available_child_count(node);
  778. if (!adc_tm->nchannels)
  779. return -EINVAL;
  780. adc_tm->channels = devm_kcalloc(dev, adc_tm->nchannels,
  781. sizeof(*adc_tm->channels), GFP_KERNEL);
  782. if (!adc_tm->channels)
  783. return -ENOMEM;
  784. channels = adc_tm->channels;
  785. adc_tm->data = of_device_get_match_data(dev);
  786. if (!adc_tm->data)
  787. adc_tm->data = &adc_tm5_data_pmic;
  788. ret = of_property_read_u32(node, "qcom,decimation", &value);
  789. if (!ret) {
  790. ret = qcom_adc5_decimation_from_dt(value, adc_tm->data->decimation);
  791. if (ret < 0) {
  792. dev_err(dev, "invalid decimation %d\n", value);
  793. return ret;
  794. }
  795. adc_tm->decimation = ret;
  796. } else {
  797. adc_tm->decimation = ADC5_DECIMATION_DEFAULT;
  798. }
  799. ret = of_property_read_u32(node, "qcom,avg-samples", &value);
  800. if (!ret) {
  801. ret = qcom_adc5_avg_samples_from_dt(value);
  802. if (ret < 0) {
  803. dev_err(dev, "invalid avg-samples %d\n", value);
  804. return ret;
  805. }
  806. adc_tm->avg_samples = ret;
  807. } else {
  808. adc_tm->avg_samples = VADC_DEF_AVG_SAMPLES;
  809. }
  810. for_each_available_child_of_node_scoped(node, child) {
  811. ret = adc_tm5_get_dt_channel_data(adc_tm, channels, child);
  812. if (ret)
  813. return ret;
  814. channels++;
  815. }
  816. return 0;
  817. }
  818. static int adc_tm5_probe(struct platform_device *pdev)
  819. {
  820. struct device_node *node = pdev->dev.of_node;
  821. struct device *dev = &pdev->dev;
  822. struct adc_tm5_chip *adc_tm;
  823. struct regmap *regmap;
  824. int ret, irq;
  825. u32 reg;
  826. regmap = dev_get_regmap(dev->parent, NULL);
  827. if (!regmap)
  828. return -ENODEV;
  829. ret = of_property_read_u32(node, "reg", &reg);
  830. if (ret)
  831. return ret;
  832. adc_tm = devm_kzalloc(&pdev->dev, sizeof(*adc_tm), GFP_KERNEL);
  833. if (!adc_tm)
  834. return -ENOMEM;
  835. adc_tm->regmap = regmap;
  836. adc_tm->dev = dev;
  837. adc_tm->base = reg;
  838. irq = platform_get_irq(pdev, 0);
  839. if (irq < 0)
  840. return irq;
  841. ret = adc_tm5_get_dt_data(adc_tm, node);
  842. if (ret)
  843. return dev_err_probe(dev, ret, "get dt data failed\n");
  844. ret = adc_tm->data->init(adc_tm);
  845. if (ret) {
  846. dev_err(dev, "adc-tm init failed\n");
  847. return ret;
  848. }
  849. ret = adc_tm5_register_tzd(adc_tm);
  850. if (ret) {
  851. dev_err(dev, "tzd register failed\n");
  852. return ret;
  853. }
  854. return devm_request_threaded_irq(dev, irq, NULL, adc_tm->data->isr,
  855. IRQF_ONESHOT, adc_tm->data->irq_name, adc_tm);
  856. }
  857. static const struct of_device_id adc_tm5_match_table[] = {
  858. {
  859. .compatible = "qcom,spmi-adc-tm5",
  860. .data = &adc_tm5_data_pmic,
  861. },
  862. {
  863. .compatible = "qcom,spmi-adc-tm-hc",
  864. .data = &adc_tm_hc_data_pmic,
  865. },
  866. {
  867. .compatible = "qcom,spmi-adc-tm5-gen2",
  868. .data = &adc_tm5_gen2_data_pmic,
  869. },
  870. { }
  871. };
  872. MODULE_DEVICE_TABLE(of, adc_tm5_match_table);
  873. static struct platform_driver adc_tm5_driver = {
  874. .driver = {
  875. .name = "qcom-spmi-adc-tm5",
  876. .of_match_table = adc_tm5_match_table,
  877. },
  878. .probe = adc_tm5_probe,
  879. };
  880. module_platform_driver(adc_tm5_driver);
  881. MODULE_DESCRIPTION("SPMI PMIC Thermal Monitor ADC driver");
  882. MODULE_LICENSE("GPL v2");