ltc2983.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
  4. * driver
  5. *
  6. * Copyright 2019 Analog Devices Inc.
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/completion.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/list.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/module.h>
  19. #include <linux/property.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/spi/spi.h>
  23. #include <asm/byteorder.h>
  24. #include <linux/unaligned.h>
  25. /* register map */
  26. #define LTC2983_STATUS_REG 0x0000
  27. #define LTC2983_TEMP_RES_START_REG 0x0010
  28. #define LTC2983_TEMP_RES_END_REG 0x005F
  29. #define LTC2983_EEPROM_KEY_REG 0x00B0
  30. #define LTC2983_EEPROM_READ_STATUS_REG 0x00D0
  31. #define LTC2983_GLOBAL_CONFIG_REG 0x00F0
  32. #define LTC2983_MULT_CHANNEL_START_REG 0x00F4
  33. #define LTC2983_MULT_CHANNEL_END_REG 0x00F7
  34. #define LTC2986_EEPROM_STATUS_REG 0x00F9
  35. #define LTC2983_MUX_CONFIG_REG 0x00FF
  36. #define LTC2983_CHAN_ASSIGN_START_REG 0x0200
  37. #define LTC2983_CHAN_ASSIGN_END_REG 0x024F
  38. #define LTC2983_CUST_SENS_TBL_START_REG 0x0250
  39. #define LTC2983_CUST_SENS_TBL_END_REG 0x03CF
  40. #define LTC2983_DIFFERENTIAL_CHAN_MIN 2
  41. #define LTC2983_MIN_CHANNELS_NR 1
  42. #define LTC2983_SLEEP 0x97
  43. #define LTC2983_CUSTOM_STEINHART_SIZE 24
  44. #define LTC2983_CUSTOM_SENSOR_ENTRY_SZ 6
  45. #define LTC2983_CUSTOM_STEINHART_ENTRY_SZ 4
  46. #define LTC2983_EEPROM_KEY 0xA53C0F5A
  47. #define LTC2983_EEPROM_WRITE_CMD 0x15
  48. #define LTC2983_EEPROM_READ_CMD 0x16
  49. #define LTC2983_EEPROM_STATUS_FAILURE_MASK GENMASK(3, 1)
  50. #define LTC2983_EEPROM_READ_FAILURE_MASK GENMASK(7, 0)
  51. #define LTC2983_EEPROM_WRITE_TIME_MS 2600
  52. #define LTC2983_EEPROM_READ_TIME_MS 20
  53. #define LTC2983_CHAN_START_ADDR(chan) \
  54. (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
  55. #define LTC2983_CHAN_RES_ADDR(chan) \
  56. (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
  57. #define LTC2983_THERMOCOUPLE_DIFF_MASK BIT(3)
  58. #define LTC2983_THERMOCOUPLE_SGL(x) \
  59. FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x)
  60. #define LTC2983_THERMOCOUPLE_OC_CURR_MASK GENMASK(1, 0)
  61. #define LTC2983_THERMOCOUPLE_OC_CURR(x) \
  62. FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x)
  63. #define LTC2983_THERMOCOUPLE_OC_CHECK_MASK BIT(2)
  64. #define LTC2983_THERMOCOUPLE_OC_CHECK(x) \
  65. FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x)
  66. #define LTC2983_THERMISTOR_DIFF_MASK BIT(2)
  67. #define LTC2983_THERMISTOR_SGL(x) \
  68. FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x)
  69. #define LTC2983_THERMISTOR_R_SHARE_MASK BIT(1)
  70. #define LTC2983_THERMISTOR_R_SHARE(x) \
  71. FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x)
  72. #define LTC2983_THERMISTOR_C_ROTATE_MASK BIT(0)
  73. #define LTC2983_THERMISTOR_C_ROTATE(x) \
  74. FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x)
  75. #define LTC2983_DIODE_DIFF_MASK BIT(2)
  76. #define LTC2983_DIODE_SGL(x) \
  77. FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x)
  78. #define LTC2983_DIODE_3_CONV_CYCLE_MASK BIT(1)
  79. #define LTC2983_DIODE_3_CONV_CYCLE(x) \
  80. FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x)
  81. #define LTC2983_DIODE_AVERAGE_ON_MASK BIT(0)
  82. #define LTC2983_DIODE_AVERAGE_ON(x) \
  83. FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x)
  84. #define LTC2983_RTD_4_WIRE_MASK BIT(3)
  85. #define LTC2983_RTD_ROTATION_MASK BIT(1)
  86. #define LTC2983_RTD_C_ROTATE(x) \
  87. FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x)
  88. #define LTC2983_RTD_KELVIN_R_SENSE_MASK GENMASK(3, 2)
  89. #define LTC2983_RTD_N_WIRES_MASK GENMASK(3, 2)
  90. #define LTC2983_RTD_N_WIRES(x) \
  91. FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x)
  92. #define LTC2983_RTD_R_SHARE_MASK BIT(0)
  93. #define LTC2983_RTD_R_SHARE(x) \
  94. FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1)
  95. #define LTC2983_COMMON_HARD_FAULT_MASK GENMASK(31, 30)
  96. #define LTC2983_COMMON_SOFT_FAULT_MASK GENMASK(27, 25)
  97. #define LTC2983_STATUS_START_MASK BIT(7)
  98. #define LTC2983_STATUS_START(x) FIELD_PREP(LTC2983_STATUS_START_MASK, x)
  99. #define LTC2983_STATUS_UP_MASK GENMASK(7, 6)
  100. #define LTC2983_STATUS_UP(reg) FIELD_GET(LTC2983_STATUS_UP_MASK, reg)
  101. #define LTC2983_STATUS_CHAN_SEL_MASK GENMASK(4, 0)
  102. #define LTC2983_STATUS_CHAN_SEL(x) \
  103. FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x)
  104. #define LTC2983_TEMP_UNITS_MASK BIT(2)
  105. #define LTC2983_TEMP_UNITS(x) FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)
  106. #define LTC2983_NOTCH_FREQ_MASK GENMASK(1, 0)
  107. #define LTC2983_NOTCH_FREQ(x) FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)
  108. #define LTC2983_RES_VALID_MASK BIT(24)
  109. #define LTC2983_DATA_MASK GENMASK(23, 0)
  110. #define LTC2983_DATA_SIGN_BIT 23
  111. #define LTC2983_CHAN_TYPE_MASK GENMASK(31, 27)
  112. #define LTC2983_CHAN_TYPE(x) FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)
  113. /* cold junction for thermocouples and rsense for rtd's and thermistor's */
  114. #define LTC2983_CHAN_ASSIGN_MASK GENMASK(26, 22)
  115. #define LTC2983_CHAN_ASSIGN(x) FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x)
  116. #define LTC2983_CUSTOM_LEN_MASK GENMASK(5, 0)
  117. #define LTC2983_CUSTOM_LEN(x) FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)
  118. #define LTC2983_CUSTOM_ADDR_MASK GENMASK(11, 6)
  119. #define LTC2983_CUSTOM_ADDR(x) FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)
  120. #define LTC2983_THERMOCOUPLE_CFG_MASK GENMASK(21, 18)
  121. #define LTC2983_THERMOCOUPLE_CFG(x) \
  122. FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x)
  123. #define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK GENMASK(31, 29)
  124. #define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK GENMASK(28, 25)
  125. #define LTC2983_RTD_CFG_MASK GENMASK(21, 18)
  126. #define LTC2983_RTD_CFG(x) FIELD_PREP(LTC2983_RTD_CFG_MASK, x)
  127. #define LTC2983_RTD_EXC_CURRENT_MASK GENMASK(17, 14)
  128. #define LTC2983_RTD_EXC_CURRENT(x) \
  129. FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x)
  130. #define LTC2983_RTD_CURVE_MASK GENMASK(13, 12)
  131. #define LTC2983_RTD_CURVE(x) FIELD_PREP(LTC2983_RTD_CURVE_MASK, x)
  132. #define LTC2983_THERMISTOR_CFG_MASK GENMASK(21, 19)
  133. #define LTC2983_THERMISTOR_CFG(x) \
  134. FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x)
  135. #define LTC2983_THERMISTOR_EXC_CURRENT_MASK GENMASK(18, 15)
  136. #define LTC2983_THERMISTOR_EXC_CURRENT(x) \
  137. FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x)
  138. #define LTC2983_DIODE_CFG_MASK GENMASK(26, 24)
  139. #define LTC2983_DIODE_CFG(x) FIELD_PREP(LTC2983_DIODE_CFG_MASK, x)
  140. #define LTC2983_DIODE_EXC_CURRENT_MASK GENMASK(23, 22)
  141. #define LTC2983_DIODE_EXC_CURRENT(x) \
  142. FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x)
  143. #define LTC2983_DIODE_IDEAL_FACTOR_MASK GENMASK(21, 0)
  144. #define LTC2983_DIODE_IDEAL_FACTOR(x) \
  145. FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x)
  146. #define LTC2983_R_SENSE_VAL_MASK GENMASK(26, 0)
  147. #define LTC2983_R_SENSE_VAL(x) FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x)
  148. #define LTC2983_ADC_SINGLE_ENDED_MASK BIT(26)
  149. #define LTC2983_ADC_SINGLE_ENDED(x) \
  150. FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x)
  151. enum {
  152. LTC2983_SENSOR_THERMOCOUPLE = 1,
  153. LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9,
  154. LTC2983_SENSOR_RTD = 10,
  155. LTC2983_SENSOR_RTD_CUSTOM = 18,
  156. LTC2983_SENSOR_THERMISTOR = 19,
  157. LTC2983_SENSOR_THERMISTOR_STEINHART = 26,
  158. LTC2983_SENSOR_THERMISTOR_CUSTOM = 27,
  159. LTC2983_SENSOR_DIODE = 28,
  160. LTC2983_SENSOR_SENSE_RESISTOR = 29,
  161. LTC2983_SENSOR_DIRECT_ADC = 30,
  162. LTC2983_SENSOR_ACTIVE_TEMP = 31,
  163. };
  164. #define to_thermocouple(_sensor) \
  165. container_of(_sensor, struct ltc2983_thermocouple, sensor)
  166. #define to_rtd(_sensor) \
  167. container_of(_sensor, struct ltc2983_rtd, sensor)
  168. #define to_thermistor(_sensor) \
  169. container_of(_sensor, struct ltc2983_thermistor, sensor)
  170. #define to_diode(_sensor) \
  171. container_of(_sensor, struct ltc2983_diode, sensor)
  172. #define to_rsense(_sensor) \
  173. container_of(_sensor, struct ltc2983_rsense, sensor)
  174. #define to_adc(_sensor) \
  175. container_of(_sensor, struct ltc2983_adc, sensor)
  176. #define to_temp(_sensor) \
  177. container_of(_sensor, struct ltc2983_temp, sensor)
  178. struct ltc2983_chip_info {
  179. const char *name;
  180. unsigned int max_channels_nr;
  181. bool has_temp;
  182. bool has_eeprom;
  183. };
  184. struct ltc2983_data {
  185. const struct ltc2983_chip_info *info;
  186. struct regmap *regmap;
  187. struct spi_device *spi;
  188. struct mutex lock;
  189. struct completion completion;
  190. struct iio_chan_spec *iio_chan;
  191. struct ltc2983_sensor **sensors;
  192. u32 mux_delay_config;
  193. u32 filter_notch_freq;
  194. u16 custom_table_size;
  195. u8 num_channels;
  196. u8 iio_channels;
  197. /*
  198. * DMA (thus cache coherency maintenance) may require the
  199. * transfer buffers to live in their own cache lines.
  200. * Holds the converted temperature
  201. */
  202. __be32 temp __aligned(IIO_DMA_MINALIGN);
  203. __be32 chan_val;
  204. __be32 eeprom_key;
  205. };
  206. struct ltc2983_sensor {
  207. int (*fault_handler)(const struct ltc2983_data *st, const u32 result);
  208. int (*assign_chan)(struct ltc2983_data *st,
  209. const struct ltc2983_sensor *sensor);
  210. /* specifies the sensor channel */
  211. u32 chan;
  212. /* sensor type */
  213. u32 type;
  214. };
  215. struct ltc2983_custom_sensor {
  216. /* raw table sensor data */
  217. void *table;
  218. size_t size;
  219. /* address offset */
  220. s8 offset;
  221. bool is_steinhart;
  222. };
  223. struct ltc2983_thermocouple {
  224. struct ltc2983_sensor sensor;
  225. struct ltc2983_custom_sensor *custom;
  226. u32 sensor_config;
  227. u32 cold_junction_chan;
  228. };
  229. struct ltc2983_rtd {
  230. struct ltc2983_sensor sensor;
  231. struct ltc2983_custom_sensor *custom;
  232. u32 sensor_config;
  233. u32 r_sense_chan;
  234. u32 excitation_current;
  235. u32 rtd_curve;
  236. };
  237. struct ltc2983_thermistor {
  238. struct ltc2983_sensor sensor;
  239. struct ltc2983_custom_sensor *custom;
  240. u32 sensor_config;
  241. u32 r_sense_chan;
  242. u32 excitation_current;
  243. };
  244. struct ltc2983_diode {
  245. struct ltc2983_sensor sensor;
  246. u32 sensor_config;
  247. u32 excitation_current;
  248. u32 ideal_factor_value;
  249. };
  250. struct ltc2983_rsense {
  251. struct ltc2983_sensor sensor;
  252. u32 r_sense_val;
  253. };
  254. struct ltc2983_adc {
  255. struct ltc2983_sensor sensor;
  256. bool single_ended;
  257. };
  258. struct ltc2983_temp {
  259. struct ltc2983_sensor sensor;
  260. struct ltc2983_custom_sensor *custom;
  261. bool single_ended;
  262. };
  263. /*
  264. * Convert to Q format numbers. These number's are integers where
  265. * the number of integer and fractional bits are specified. The resolution
  266. * is given by 1/@resolution and tell us the number of fractional bits. For
  267. * instance a resolution of 2^-10 means we have 10 fractional bits.
  268. */
  269. static u32 __convert_to_raw(const u64 val, const u32 resolution)
  270. {
  271. u64 __res = val * resolution;
  272. /* all values are multiplied by 1000000 to remove the fraction */
  273. do_div(__res, 1000000);
  274. return __res;
  275. }
  276. static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
  277. {
  278. s64 __res = -(s32)val;
  279. __res = __convert_to_raw(__res, resolution);
  280. return (u32)-__res;
  281. }
  282. static int __ltc2983_fault_handler(const struct ltc2983_data *st,
  283. const u32 result, const u32 hard_mask,
  284. const u32 soft_mask)
  285. {
  286. const struct device *dev = &st->spi->dev;
  287. if (result & hard_mask) {
  288. dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
  289. return -EIO;
  290. } else if (result & soft_mask) {
  291. /* just print a warning */
  292. dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
  293. }
  294. return 0;
  295. }
  296. static int __ltc2983_chan_assign_common(struct ltc2983_data *st,
  297. const struct ltc2983_sensor *sensor,
  298. u32 chan_val)
  299. {
  300. u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan);
  301. chan_val |= LTC2983_CHAN_TYPE(sensor->type);
  302. dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg,
  303. chan_val);
  304. st->chan_val = cpu_to_be32(chan_val);
  305. return regmap_bulk_write(st->regmap, reg, &st->chan_val,
  306. sizeof(st->chan_val));
  307. }
  308. static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
  309. struct ltc2983_custom_sensor *custom,
  310. u32 *chan_val)
  311. {
  312. u32 reg;
  313. u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ :
  314. LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
  315. const struct device *dev = &st->spi->dev;
  316. /*
  317. * custom->size holds the raw size of the table. However, when
  318. * configuring the sensor channel, we must write the number of
  319. * entries of the table minus 1. For steinhart sensors 0 is written
  320. * since the size is constant!
  321. */
  322. const u8 len = custom->is_steinhart ? 0 :
  323. (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
  324. /*
  325. * Check if the offset was assigned already. It should be for steinhart
  326. * sensors. When coming from sleep, it should be assigned for all.
  327. */
  328. if (custom->offset < 0) {
  329. /*
  330. * This needs to be done again here because, from the moment
  331. * when this test was done (successfully) for this custom
  332. * sensor, a steinhart sensor might have been added changing
  333. * custom_table_size...
  334. */
  335. if (st->custom_table_size + custom->size >
  336. (LTC2983_CUST_SENS_TBL_END_REG -
  337. LTC2983_CUST_SENS_TBL_START_REG) + 1) {
  338. dev_err(dev,
  339. "Not space left(%d) for new custom sensor(%zu)",
  340. st->custom_table_size,
  341. custom->size);
  342. return -EINVAL;
  343. }
  344. custom->offset = st->custom_table_size /
  345. LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
  346. st->custom_table_size += custom->size;
  347. }
  348. reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;
  349. *chan_val |= LTC2983_CUSTOM_LEN(len);
  350. *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset);
  351. dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
  352. reg, custom->offset,
  353. custom->size);
  354. /* write custom sensor table */
  355. return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
  356. }
  357. static struct ltc2983_custom_sensor *
  358. __ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle *fn,
  359. const char *propname, const bool is_steinhart,
  360. const u32 resolution, const bool has_signed)
  361. {
  362. struct ltc2983_custom_sensor *new_custom;
  363. struct device *dev = &st->spi->dev;
  364. /*
  365. * For custom steinhart, the full u32 is taken. For all the others
  366. * the MSB is discarded.
  367. */
  368. const u8 n_size = is_steinhart ? 4 : 3;
  369. u8 index, n_entries;
  370. int ret;
  371. if (is_steinhart)
  372. n_entries = fwnode_property_count_u32(fn, propname);
  373. else
  374. n_entries = fwnode_property_count_u64(fn, propname);
  375. /* n_entries must be an even number */
  376. if (!n_entries || (n_entries % 2) != 0)
  377. return dev_err_ptr_probe(dev, -EINVAL,
  378. "Number of entries either 0 or not even\n");
  379. new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
  380. if (!new_custom)
  381. return ERR_PTR(-ENOMEM);
  382. new_custom->size = n_entries * n_size;
  383. /* check Steinhart size */
  384. if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE)
  385. return dev_err_ptr_probe(dev, -EINVAL,
  386. "Steinhart sensors size(%zu) must be %u\n",
  387. new_custom->size, LTC2983_CUSTOM_STEINHART_SIZE);
  388. /* Check space on the table. */
  389. if (st->custom_table_size + new_custom->size >
  390. (LTC2983_CUST_SENS_TBL_END_REG - LTC2983_CUST_SENS_TBL_START_REG) + 1)
  391. return dev_err_ptr_probe(dev, -EINVAL,
  392. "No space left(%d) for new custom sensor(%zu)\n",
  393. st->custom_table_size, new_custom->size);
  394. /* allocate the table */
  395. if (is_steinhart)
  396. new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u32), GFP_KERNEL);
  397. else
  398. new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u64), GFP_KERNEL);
  399. if (!new_custom->table)
  400. return ERR_PTR(-ENOMEM);
  401. /*
  402. * Steinhart sensors are configured with raw values in the firmware
  403. * node. For the other sensors we must convert the value to raw.
  404. * The odd index's correspond to temperatures and always have 1/1024
  405. * of resolution. Temperatures also come in Kelvin, so signed values
  406. * are not possible.
  407. */
  408. if (is_steinhart) {
  409. ret = fwnode_property_read_u32_array(fn, propname, new_custom->table, n_entries);
  410. if (ret < 0)
  411. return ERR_PTR(ret);
  412. cpu_to_be32_array(new_custom->table, new_custom->table, n_entries);
  413. } else {
  414. ret = fwnode_property_read_u64_array(fn, propname, new_custom->table, n_entries);
  415. if (ret < 0)
  416. return ERR_PTR(ret);
  417. for (index = 0; index < n_entries; index++) {
  418. u64 temp = ((u64 *)new_custom->table)[index];
  419. if ((index % 2) != 0)
  420. temp = __convert_to_raw(temp, 1024);
  421. else if (has_signed && (s64)temp < 0)
  422. temp = __convert_to_raw_sign(temp, resolution);
  423. else
  424. temp = __convert_to_raw(temp, resolution);
  425. put_unaligned_be24(temp, new_custom->table + index * 3);
  426. }
  427. }
  428. new_custom->is_steinhart = is_steinhart;
  429. /*
  430. * This is done to first add all the steinhart sensors to the table,
  431. * in order to maximize the table usage. If we mix adding steinhart
  432. * with the other sensors, we might have to do some roundup to make
  433. * sure that sensor_addr - 0x250(start address) is a multiple of 4
  434. * (for steinhart), and a multiple of 6 for all the other sensors.
  435. * Since we have const 24 bytes for steinhart sensors and 24 is
  436. * also a multiple of 6, we guarantee that the first non-steinhart
  437. * sensor will sit in a correct address without the need of filling
  438. * addresses.
  439. */
  440. if (is_steinhart) {
  441. new_custom->offset = st->custom_table_size /
  442. LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
  443. st->custom_table_size += new_custom->size;
  444. } else {
  445. /* mark as unset. This is checked later on the assign phase */
  446. new_custom->offset = -1;
  447. }
  448. return new_custom;
  449. }
  450. static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
  451. const u32 result)
  452. {
  453. return __ltc2983_fault_handler(st, result,
  454. LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
  455. LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
  456. }
  457. static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
  458. const u32 result)
  459. {
  460. return __ltc2983_fault_handler(st, result,
  461. LTC2983_COMMON_HARD_FAULT_MASK,
  462. LTC2983_COMMON_SOFT_FAULT_MASK);
  463. }
  464. static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
  465. const struct ltc2983_sensor *sensor)
  466. {
  467. struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
  468. u32 chan_val;
  469. chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
  470. chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);
  471. if (thermo->custom) {
  472. int ret;
  473. ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
  474. &chan_val);
  475. if (ret)
  476. return ret;
  477. }
  478. return __ltc2983_chan_assign_common(st, sensor, chan_val);
  479. }
  480. static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
  481. const struct ltc2983_sensor *sensor)
  482. {
  483. struct ltc2983_rtd *rtd = to_rtd(sensor);
  484. u32 chan_val;
  485. chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan);
  486. chan_val |= LTC2983_RTD_CFG(rtd->sensor_config);
  487. chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current);
  488. chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve);
  489. if (rtd->custom) {
  490. int ret;
  491. ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
  492. &chan_val);
  493. if (ret)
  494. return ret;
  495. }
  496. return __ltc2983_chan_assign_common(st, sensor, chan_val);
  497. }
  498. static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
  499. const struct ltc2983_sensor *sensor)
  500. {
  501. struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
  502. u32 chan_val;
  503. chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
  504. chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
  505. chan_val |=
  506. LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);
  507. if (thermistor->custom) {
  508. int ret;
  509. ret = __ltc2983_chan_custom_sensor_assign(st,
  510. thermistor->custom,
  511. &chan_val);
  512. if (ret)
  513. return ret;
  514. }
  515. return __ltc2983_chan_assign_common(st, sensor, chan_val);
  516. }
  517. static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
  518. const struct ltc2983_sensor *sensor)
  519. {
  520. struct ltc2983_diode *diode = to_diode(sensor);
  521. u32 chan_val;
  522. chan_val = LTC2983_DIODE_CFG(diode->sensor_config);
  523. chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current);
  524. chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value);
  525. return __ltc2983_chan_assign_common(st, sensor, chan_val);
  526. }
  527. static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
  528. const struct ltc2983_sensor *sensor)
  529. {
  530. struct ltc2983_rsense *rsense = to_rsense(sensor);
  531. u32 chan_val;
  532. chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);
  533. return __ltc2983_chan_assign_common(st, sensor, chan_val);
  534. }
  535. static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
  536. const struct ltc2983_sensor *sensor)
  537. {
  538. struct ltc2983_adc *adc = to_adc(sensor);
  539. u32 chan_val;
  540. chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);
  541. return __ltc2983_chan_assign_common(st, sensor, chan_val);
  542. }
  543. static int ltc2983_temp_assign_chan(struct ltc2983_data *st,
  544. const struct ltc2983_sensor *sensor)
  545. {
  546. struct ltc2983_temp *temp = to_temp(sensor);
  547. u32 chan_val;
  548. int ret;
  549. chan_val = LTC2983_ADC_SINGLE_ENDED(temp->single_ended);
  550. ret = __ltc2983_chan_custom_sensor_assign(st, temp->custom, &chan_val);
  551. if (ret)
  552. return ret;
  553. return __ltc2983_chan_assign_common(st, sensor, chan_val);
  554. }
  555. static struct ltc2983_sensor *
  556. ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data *st,
  557. const struct ltc2983_sensor *sensor)
  558. {
  559. struct ltc2983_thermocouple *thermo;
  560. u32 oc_current;
  561. int ret;
  562. thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL);
  563. if (!thermo)
  564. return ERR_PTR(-ENOMEM);
  565. if (fwnode_property_read_bool(child, "adi,single-ended"))
  566. thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);
  567. ret = fwnode_property_read_u32(child, "adi,sensor-oc-current-microamp", &oc_current);
  568. if (!ret) {
  569. switch (oc_current) {
  570. case 10:
  571. thermo->sensor_config |=
  572. LTC2983_THERMOCOUPLE_OC_CURR(0);
  573. break;
  574. case 100:
  575. thermo->sensor_config |=
  576. LTC2983_THERMOCOUPLE_OC_CURR(1);
  577. break;
  578. case 500:
  579. thermo->sensor_config |=
  580. LTC2983_THERMOCOUPLE_OC_CURR(2);
  581. break;
  582. case 1000:
  583. thermo->sensor_config |=
  584. LTC2983_THERMOCOUPLE_OC_CURR(3);
  585. break;
  586. default:
  587. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  588. "Invalid open circuit current:%u\n",
  589. oc_current);
  590. }
  591. thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
  592. }
  593. /* validate channel index */
  594. if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) &&
  595. sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
  596. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  597. "Invalid chann:%d for differential thermocouple\n",
  598. sensor->chan);
  599. struct fwnode_handle *ref __free(fwnode_handle) =
  600. fwnode_find_reference(child, "adi,cold-junction-handle", 0);
  601. if (IS_ERR(ref)) {
  602. ref = NULL;
  603. } else {
  604. ret = fwnode_property_read_u32(ref, "reg", &thermo->cold_junction_chan);
  605. if (ret)
  606. /*
  607. * This would be catched later but we can just return
  608. * the error right away.
  609. */
  610. return dev_err_ptr_probe(&st->spi->dev, ret,
  611. "Property reg must be given\n");
  612. }
  613. /* check custom sensor */
  614. if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
  615. const char *propname = "adi,custom-thermocouple";
  616. thermo->custom = __ltc2983_custom_sensor_new(st, child,
  617. propname, false,
  618. 16384, true);
  619. if (IS_ERR(thermo->custom))
  620. return ERR_CAST(thermo->custom);
  621. }
  622. /* set common parameters */
  623. thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
  624. thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;
  625. return &thermo->sensor;
  626. }
  627. static struct ltc2983_sensor *
  628. ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st,
  629. const struct ltc2983_sensor *sensor)
  630. {
  631. struct ltc2983_rtd *rtd;
  632. int ret = 0;
  633. struct device *dev = &st->spi->dev;
  634. u32 excitation_current = 0, n_wires = 0;
  635. rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
  636. if (!rtd)
  637. return ERR_PTR(-ENOMEM);
  638. struct fwnode_handle *ref __free(fwnode_handle) =
  639. fwnode_find_reference(child, "adi,rsense-handle", 0);
  640. if (IS_ERR(ref))
  641. return dev_err_cast_probe(dev, ref,
  642. "Property adi,rsense-handle missing or invalid\n");
  643. ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan);
  644. if (ret)
  645. return dev_err_ptr_probe(dev, ret,
  646. "Property reg must be given\n");
  647. ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires);
  648. if (!ret) {
  649. switch (n_wires) {
  650. case 2:
  651. rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
  652. break;
  653. case 3:
  654. rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
  655. break;
  656. case 4:
  657. rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
  658. break;
  659. case 5:
  660. /* 4 wires, Kelvin Rsense */
  661. rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
  662. break;
  663. default:
  664. return dev_err_ptr_probe(dev, -EINVAL,
  665. "Invalid number of wires:%u\n",
  666. n_wires);
  667. }
  668. }
  669. if (fwnode_property_read_bool(child, "adi,rsense-share")) {
  670. /* Current rotation is only available with rsense sharing */
  671. if (fwnode_property_read_bool(child, "adi,current-rotate")) {
  672. if (n_wires == 2 || n_wires == 3)
  673. return dev_err_ptr_probe(dev, -EINVAL,
  674. "Rotation not allowed for 2/3 Wire RTDs\n");
  675. rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
  676. } else {
  677. rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
  678. }
  679. }
  680. /*
  681. * rtd channel indexes are a bit more complicated to validate.
  682. * For 4wire RTD with rotation, the channel selection cannot be
  683. * >=19 since the chann + 1 is used in this configuration.
  684. * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
  685. * <=1 since chanel - 1 and channel - 2 are used.
  686. */
  687. if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
  688. /* 4-wire */
  689. u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
  690. max = st->info->max_channels_nr;
  691. if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
  692. max = st->info->max_channels_nr - 1;
  693. if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
  694. == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
  695. (rtd->r_sense_chan <= min))
  696. /* kelvin rsense*/
  697. return dev_err_ptr_probe(dev, -EINVAL,
  698. "Invalid rsense chann:%d to use in kelvin rsense\n",
  699. rtd->r_sense_chan);
  700. if (sensor->chan < min || sensor->chan > max)
  701. return dev_err_ptr_probe(dev, -EINVAL,
  702. "Invalid chann:%d for the rtd config\n",
  703. sensor->chan);
  704. } else {
  705. /* same as differential case */
  706. if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
  707. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  708. "Invalid chann:%d for RTD\n",
  709. sensor->chan);
  710. }
  711. /* check custom sensor */
  712. if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
  713. rtd->custom = __ltc2983_custom_sensor_new(st, child,
  714. "adi,custom-rtd",
  715. false, 2048, false);
  716. if (IS_ERR(rtd->custom))
  717. return ERR_CAST(rtd->custom);
  718. }
  719. /* set common parameters */
  720. rtd->sensor.fault_handler = ltc2983_common_fault_handler;
  721. rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;
  722. ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
  723. &excitation_current);
  724. if (ret) {
  725. /* default to 5uA */
  726. rtd->excitation_current = 1;
  727. } else {
  728. switch (excitation_current) {
  729. case 5:
  730. rtd->excitation_current = 0x01;
  731. break;
  732. case 10:
  733. rtd->excitation_current = 0x02;
  734. break;
  735. case 25:
  736. rtd->excitation_current = 0x03;
  737. break;
  738. case 50:
  739. rtd->excitation_current = 0x04;
  740. break;
  741. case 100:
  742. rtd->excitation_current = 0x05;
  743. break;
  744. case 250:
  745. rtd->excitation_current = 0x06;
  746. break;
  747. case 500:
  748. rtd->excitation_current = 0x07;
  749. break;
  750. case 1000:
  751. rtd->excitation_current = 0x08;
  752. break;
  753. default:
  754. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  755. "Invalid value for excitation current(%u)\n",
  756. excitation_current);
  757. }
  758. }
  759. fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
  760. return &rtd->sensor;
  761. }
  762. static struct ltc2983_sensor *
  763. ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *st,
  764. const struct ltc2983_sensor *sensor)
  765. {
  766. struct ltc2983_thermistor *thermistor;
  767. struct device *dev = &st->spi->dev;
  768. u32 excitation_current = 0;
  769. int ret = 0;
  770. thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
  771. if (!thermistor)
  772. return ERR_PTR(-ENOMEM);
  773. struct fwnode_handle *ref __free(fwnode_handle) =
  774. fwnode_find_reference(child, "adi,rsense-handle", 0);
  775. if (IS_ERR(ref))
  776. return dev_err_cast_probe(dev, ref,
  777. "Property adi,rsense-handle missing or invalid\n");
  778. ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan);
  779. if (ret)
  780. return dev_err_ptr_probe(dev, ret,
  781. "rsense channel must be configured...\n");
  782. if (fwnode_property_read_bool(child, "adi,single-ended")) {
  783. thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1);
  784. } else if (fwnode_property_read_bool(child, "adi,rsense-share")) {
  785. /* rotation is only possible if sharing rsense */
  786. if (fwnode_property_read_bool(child, "adi,current-rotate"))
  787. thermistor->sensor_config =
  788. LTC2983_THERMISTOR_C_ROTATE(1);
  789. else
  790. thermistor->sensor_config =
  791. LTC2983_THERMISTOR_R_SHARE(1);
  792. }
  793. /* validate channel index */
  794. if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) &&
  795. sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
  796. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  797. "Invalid chann:%d for differential thermistor\n",
  798. sensor->chan);
  799. /* check custom sensor */
  800. if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
  801. bool steinhart = false;
  802. const char *propname;
  803. if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
  804. steinhart = true;
  805. propname = "adi,custom-steinhart";
  806. } else {
  807. propname = "adi,custom-thermistor";
  808. }
  809. thermistor->custom = __ltc2983_custom_sensor_new(st, child,
  810. propname,
  811. steinhart,
  812. 64, false);
  813. if (IS_ERR(thermistor->custom))
  814. return ERR_CAST(thermistor->custom);
  815. }
  816. /* set common parameters */
  817. thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
  818. thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;
  819. ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp",
  820. &excitation_current);
  821. if (ret) {
  822. /* Auto range is not allowed for custom sensors */
  823. if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
  824. /* default to 1uA */
  825. thermistor->excitation_current = 0x03;
  826. else
  827. /* default to auto-range */
  828. thermistor->excitation_current = 0x0c;
  829. } else {
  830. switch (excitation_current) {
  831. case 0:
  832. /* auto range */
  833. if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
  834. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  835. "Auto Range not allowed for custom sensors\n");
  836. thermistor->excitation_current = 0x0c;
  837. break;
  838. case 250:
  839. thermistor->excitation_current = 0x01;
  840. break;
  841. case 500:
  842. thermistor->excitation_current = 0x02;
  843. break;
  844. case 1000:
  845. thermistor->excitation_current = 0x03;
  846. break;
  847. case 5000:
  848. thermistor->excitation_current = 0x04;
  849. break;
  850. case 10000:
  851. thermistor->excitation_current = 0x05;
  852. break;
  853. case 25000:
  854. thermistor->excitation_current = 0x06;
  855. break;
  856. case 50000:
  857. thermistor->excitation_current = 0x07;
  858. break;
  859. case 100000:
  860. thermistor->excitation_current = 0x08;
  861. break;
  862. case 250000:
  863. thermistor->excitation_current = 0x09;
  864. break;
  865. case 500000:
  866. thermistor->excitation_current = 0x0a;
  867. break;
  868. case 1000000:
  869. thermistor->excitation_current = 0x0b;
  870. break;
  871. default:
  872. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  873. "Invalid value for excitation current(%u)\n",
  874. excitation_current);
  875. }
  876. }
  877. return &thermistor->sensor;
  878. }
  879. static struct ltc2983_sensor *
  880. ltc2983_diode_new(const struct fwnode_handle *child, const struct ltc2983_data *st,
  881. const struct ltc2983_sensor *sensor)
  882. {
  883. struct ltc2983_diode *diode;
  884. u32 temp = 0, excitation_current = 0;
  885. int ret;
  886. diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL);
  887. if (!diode)
  888. return ERR_PTR(-ENOMEM);
  889. if (fwnode_property_read_bool(child, "adi,single-ended"))
  890. diode->sensor_config = LTC2983_DIODE_SGL(1);
  891. if (fwnode_property_read_bool(child, "adi,three-conversion-cycles"))
  892. diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);
  893. if (fwnode_property_read_bool(child, "adi,average-on"))
  894. diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);
  895. /* validate channel index */
  896. if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) &&
  897. sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
  898. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  899. "Invalid chann:%d for differential thermistor\n",
  900. sensor->chan);
  901. /* set common parameters */
  902. diode->sensor.fault_handler = ltc2983_common_fault_handler;
  903. diode->sensor.assign_chan = ltc2983_diode_assign_chan;
  904. ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
  905. &excitation_current);
  906. if (!ret) {
  907. switch (excitation_current) {
  908. case 10:
  909. diode->excitation_current = 0x00;
  910. break;
  911. case 20:
  912. diode->excitation_current = 0x01;
  913. break;
  914. case 40:
  915. diode->excitation_current = 0x02;
  916. break;
  917. case 80:
  918. diode->excitation_current = 0x03;
  919. break;
  920. default:
  921. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  922. "Invalid value for excitation current(%u)\n",
  923. excitation_current);
  924. }
  925. }
  926. fwnode_property_read_u32(child, "adi,ideal-factor-value", &temp);
  927. /* 2^20 resolution */
  928. diode->ideal_factor_value = __convert_to_raw(temp, 1048576);
  929. return &diode->sensor;
  930. }
  931. static struct ltc2983_sensor *ltc2983_r_sense_new(struct fwnode_handle *child,
  932. struct ltc2983_data *st,
  933. const struct ltc2983_sensor *sensor)
  934. {
  935. struct ltc2983_rsense *rsense;
  936. int ret;
  937. u32 temp;
  938. rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL);
  939. if (!rsense)
  940. return ERR_PTR(-ENOMEM);
  941. /* validate channel index */
  942. if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
  943. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  944. "Invalid chann:%d for r_sense\n",
  945. sensor->chan);
  946. ret = fwnode_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
  947. if (ret)
  948. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  949. "Property adi,rsense-val-milli-ohms missing\n");
  950. /*
  951. * Times 1000 because we have milli-ohms and __convert_to_raw
  952. * expects scales of 1000000 which are used for all other
  953. * properties.
  954. * 2^10 resolution
  955. */
  956. rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);
  957. /* set common parameters */
  958. rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;
  959. return &rsense->sensor;
  960. }
  961. static struct ltc2983_sensor *ltc2983_adc_new(struct fwnode_handle *child,
  962. struct ltc2983_data *st,
  963. const struct ltc2983_sensor *sensor)
  964. {
  965. struct ltc2983_adc *adc;
  966. adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL);
  967. if (!adc)
  968. return ERR_PTR(-ENOMEM);
  969. if (fwnode_property_read_bool(child, "adi,single-ended"))
  970. adc->single_ended = true;
  971. if (!adc->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
  972. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  973. "Invalid chan:%d for differential adc\n",
  974. sensor->chan);
  975. /* set common parameters */
  976. adc->sensor.assign_chan = ltc2983_adc_assign_chan;
  977. adc->sensor.fault_handler = ltc2983_common_fault_handler;
  978. return &adc->sensor;
  979. }
  980. static struct ltc2983_sensor *ltc2983_temp_new(struct fwnode_handle *child,
  981. struct ltc2983_data *st,
  982. const struct ltc2983_sensor *sensor)
  983. {
  984. struct ltc2983_temp *temp;
  985. temp = devm_kzalloc(&st->spi->dev, sizeof(*temp), GFP_KERNEL);
  986. if (!temp)
  987. return ERR_PTR(-ENOMEM);
  988. if (fwnode_property_read_bool(child, "adi,single-ended"))
  989. temp->single_ended = true;
  990. if (!temp->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
  991. return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
  992. "Invalid chan:%d for differential temp\n",
  993. sensor->chan);
  994. temp->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-temp",
  995. false, 4096, true);
  996. if (IS_ERR(temp->custom))
  997. return ERR_CAST(temp->custom);
  998. /* set common parameters */
  999. temp->sensor.assign_chan = ltc2983_temp_assign_chan;
  1000. temp->sensor.fault_handler = ltc2983_common_fault_handler;
  1001. return &temp->sensor;
  1002. }
  1003. static int ltc2983_chan_read(struct ltc2983_data *st,
  1004. const struct ltc2983_sensor *sensor, int *val)
  1005. {
  1006. u32 start_conversion = 0;
  1007. int ret;
  1008. unsigned long time;
  1009. start_conversion = LTC2983_STATUS_START(true);
  1010. start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan);
  1011. dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n",
  1012. sensor->chan, start_conversion);
  1013. /* start conversion */
  1014. ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion);
  1015. if (ret)
  1016. return ret;
  1017. reinit_completion(&st->completion);
  1018. /*
  1019. * wait for conversion to complete.
  1020. * 300 ms should be more than enough to complete the conversion.
  1021. * Depending on the sensor configuration, there are 2/3 conversions
  1022. * cycles of 82ms.
  1023. */
  1024. time = wait_for_completion_timeout(&st->completion,
  1025. msecs_to_jiffies(300));
  1026. if (!time) {
  1027. dev_warn(&st->spi->dev, "Conversion timed out\n");
  1028. return -ETIMEDOUT;
  1029. }
  1030. /* read the converted data */
  1031. ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan),
  1032. &st->temp, sizeof(st->temp));
  1033. if (ret)
  1034. return ret;
  1035. *val = __be32_to_cpu(st->temp);
  1036. if (!(LTC2983_RES_VALID_MASK & *val)) {
  1037. dev_err(&st->spi->dev, "Invalid conversion detected\n");
  1038. return -EIO;
  1039. }
  1040. ret = sensor->fault_handler(st, *val);
  1041. if (ret)
  1042. return ret;
  1043. *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
  1044. return 0;
  1045. }
  1046. static int ltc2983_read_raw(struct iio_dev *indio_dev,
  1047. struct iio_chan_spec const *chan,
  1048. int *val, int *val2, long mask)
  1049. {
  1050. struct ltc2983_data *st = iio_priv(indio_dev);
  1051. int ret;
  1052. /* sanity check */
  1053. if (chan->address >= st->num_channels) {
  1054. dev_err(&st->spi->dev, "Invalid chan address:%ld",
  1055. chan->address);
  1056. return -EINVAL;
  1057. }
  1058. switch (mask) {
  1059. case IIO_CHAN_INFO_RAW:
  1060. mutex_lock(&st->lock);
  1061. ret = ltc2983_chan_read(st, st->sensors[chan->address], val);
  1062. mutex_unlock(&st->lock);
  1063. return ret ?: IIO_VAL_INT;
  1064. case IIO_CHAN_INFO_SCALE:
  1065. switch (chan->type) {
  1066. case IIO_TEMP:
  1067. /* value in milli degrees */
  1068. *val = 1000;
  1069. /* 2^10 */
  1070. *val2 = 1024;
  1071. return IIO_VAL_FRACTIONAL;
  1072. case IIO_VOLTAGE:
  1073. /* value in millivolt */
  1074. *val = 1000;
  1075. /* 2^21 */
  1076. *val2 = 2097152;
  1077. return IIO_VAL_FRACTIONAL;
  1078. default:
  1079. return -EINVAL;
  1080. }
  1081. }
  1082. return -EINVAL;
  1083. }
  1084. static int ltc2983_reg_access(struct iio_dev *indio_dev,
  1085. unsigned int reg,
  1086. unsigned int writeval,
  1087. unsigned int *readval)
  1088. {
  1089. struct ltc2983_data *st = iio_priv(indio_dev);
  1090. if (readval)
  1091. return regmap_read(st->regmap, reg, readval);
  1092. return regmap_write(st->regmap, reg, writeval);
  1093. }
  1094. static irqreturn_t ltc2983_irq_handler(int irq, void *data)
  1095. {
  1096. struct ltc2983_data *st = data;
  1097. complete(&st->completion);
  1098. return IRQ_HANDLED;
  1099. }
  1100. #define LTC2983_CHAN(__type, index, __address) ({ \
  1101. struct iio_chan_spec __chan = { \
  1102. .type = __type, \
  1103. .indexed = 1, \
  1104. .channel = index, \
  1105. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  1106. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  1107. .address = __address, \
  1108. }; \
  1109. __chan; \
  1110. })
  1111. static int ltc2983_parse_fw(struct ltc2983_data *st)
  1112. {
  1113. struct device *dev = &st->spi->dev;
  1114. int ret, chan = 0, channel_avail_mask = 0;
  1115. device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config);
  1116. device_property_read_u32(dev, "adi,filter-notch-freq", &st->filter_notch_freq);
  1117. st->num_channels = device_get_child_node_count(dev);
  1118. if (!st->num_channels)
  1119. return dev_err_probe(&st->spi->dev, -EINVAL,
  1120. "At least one channel must be given!\n");
  1121. st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
  1122. GFP_KERNEL);
  1123. if (!st->sensors)
  1124. return -ENOMEM;
  1125. st->iio_channels = st->num_channels;
  1126. device_for_each_child_node_scoped(dev, child) {
  1127. struct ltc2983_sensor sensor;
  1128. ret = fwnode_property_read_u32(child, "reg", &sensor.chan);
  1129. if (ret)
  1130. return dev_err_probe(dev, ret,
  1131. "reg property must given for child nodes\n");
  1132. /* check if we have a valid channel */
  1133. if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
  1134. sensor.chan > st->info->max_channels_nr)
  1135. return dev_err_probe(dev, -EINVAL,
  1136. "chan:%d must be from %u to %u\n",
  1137. sensor.chan,
  1138. LTC2983_MIN_CHANNELS_NR,
  1139. st->info->max_channels_nr);
  1140. if (channel_avail_mask & BIT(sensor.chan))
  1141. return dev_err_probe(dev, -EINVAL,
  1142. "chan:%d already in use\n",
  1143. sensor.chan);
  1144. ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type);
  1145. if (ret)
  1146. return dev_err_probe(dev, ret,
  1147. "adi,sensor-type property must given for child nodes\n");
  1148. dev_dbg(dev, "Create new sensor, type %u, chann %u",
  1149. sensor.type, sensor.chan);
  1150. if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
  1151. sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
  1152. st->sensors[chan] = ltc2983_thermocouple_new(child, st,
  1153. &sensor);
  1154. } else if (sensor.type >= LTC2983_SENSOR_RTD &&
  1155. sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) {
  1156. st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor);
  1157. } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR &&
  1158. sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) {
  1159. st->sensors[chan] = ltc2983_thermistor_new(child, st,
  1160. &sensor);
  1161. } else if (sensor.type == LTC2983_SENSOR_DIODE) {
  1162. st->sensors[chan] = ltc2983_diode_new(child, st,
  1163. &sensor);
  1164. } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
  1165. st->sensors[chan] = ltc2983_r_sense_new(child, st,
  1166. &sensor);
  1167. /* don't add rsense to iio */
  1168. st->iio_channels--;
  1169. } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) {
  1170. st->sensors[chan] = ltc2983_adc_new(child, st, &sensor);
  1171. } else if (st->info->has_temp &&
  1172. sensor.type == LTC2983_SENSOR_ACTIVE_TEMP) {
  1173. st->sensors[chan] = ltc2983_temp_new(child, st, &sensor);
  1174. } else {
  1175. return dev_err_probe(dev, -EINVAL,
  1176. "Unknown sensor type %d\n",
  1177. sensor.type);
  1178. }
  1179. if (IS_ERR(st->sensors[chan]))
  1180. return dev_err_probe(dev, PTR_ERR(st->sensors[chan]),
  1181. "Failed to create sensor\n");
  1182. /* set generic sensor parameters */
  1183. st->sensors[chan]->chan = sensor.chan;
  1184. st->sensors[chan]->type = sensor.type;
  1185. channel_avail_mask |= BIT(sensor.chan);
  1186. chan++;
  1187. }
  1188. return 0;
  1189. }
  1190. static int ltc2983_eeprom_cmd(struct ltc2983_data *st, unsigned int cmd,
  1191. unsigned int wait_time, unsigned int status_reg,
  1192. unsigned long status_fail_mask)
  1193. {
  1194. unsigned long time;
  1195. unsigned int val;
  1196. int ret;
  1197. ret = regmap_bulk_write(st->regmap, LTC2983_EEPROM_KEY_REG,
  1198. &st->eeprom_key, sizeof(st->eeprom_key));
  1199. if (ret)
  1200. return ret;
  1201. reinit_completion(&st->completion);
  1202. ret = regmap_write(st->regmap, LTC2983_STATUS_REG,
  1203. LTC2983_STATUS_START(true) | cmd);
  1204. if (ret)
  1205. return ret;
  1206. time = wait_for_completion_timeout(&st->completion,
  1207. msecs_to_jiffies(wait_time));
  1208. if (!time)
  1209. return dev_err_probe(&st->spi->dev, -ETIMEDOUT,
  1210. "EEPROM command timed out\n");
  1211. ret = regmap_read(st->regmap, status_reg, &val);
  1212. if (ret)
  1213. return ret;
  1214. if (val & status_fail_mask)
  1215. return dev_err_probe(&st->spi->dev, -EINVAL,
  1216. "EEPROM command failed: 0x%02X\n", val);
  1217. return 0;
  1218. }
  1219. static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
  1220. {
  1221. u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status;
  1222. int ret;
  1223. /* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */
  1224. ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status,
  1225. LTC2983_STATUS_UP(status) == 1, 25000,
  1226. 25000 * 10);
  1227. if (ret)
  1228. return dev_err_probe(&st->spi->dev, ret,
  1229. "Device startup timed out\n");
  1230. ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
  1231. LTC2983_NOTCH_FREQ_MASK,
  1232. LTC2983_NOTCH_FREQ(st->filter_notch_freq));
  1233. if (ret)
  1234. return ret;
  1235. ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
  1236. st->mux_delay_config);
  1237. if (ret)
  1238. return ret;
  1239. if (st->info->has_eeprom && !assign_iio) {
  1240. ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_READ_CMD,
  1241. LTC2983_EEPROM_READ_TIME_MS,
  1242. LTC2983_EEPROM_READ_STATUS_REG,
  1243. LTC2983_EEPROM_READ_FAILURE_MASK);
  1244. if (!ret)
  1245. return 0;
  1246. }
  1247. for (chan = 0; chan < st->num_channels; chan++) {
  1248. u32 chan_type = 0, *iio_chan;
  1249. ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
  1250. if (ret)
  1251. return ret;
  1252. /*
  1253. * The assign_iio flag is necessary for when the device is
  1254. * coming out of sleep. In that case, we just need to
  1255. * re-configure the device channels.
  1256. * We also don't assign iio channels for rsense.
  1257. */
  1258. if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
  1259. !assign_iio)
  1260. continue;
  1261. /* assign iio channel */
  1262. if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) {
  1263. chan_type = IIO_TEMP;
  1264. iio_chan = &iio_chan_t;
  1265. } else {
  1266. chan_type = IIO_VOLTAGE;
  1267. iio_chan = &iio_chan_v;
  1268. }
  1269. /*
  1270. * add chan as the iio .address so that, we can directly
  1271. * reference the sensor given the iio_chan_spec
  1272. */
  1273. st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
  1274. chan);
  1275. }
  1276. return 0;
  1277. }
  1278. static const struct regmap_range ltc2983_reg_ranges[] = {
  1279. regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG),
  1280. regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG),
  1281. regmap_reg_range(LTC2983_EEPROM_KEY_REG, LTC2983_EEPROM_KEY_REG),
  1282. regmap_reg_range(LTC2983_EEPROM_READ_STATUS_REG,
  1283. LTC2983_EEPROM_READ_STATUS_REG),
  1284. regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG),
  1285. regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG,
  1286. LTC2983_MULT_CHANNEL_END_REG),
  1287. regmap_reg_range(LTC2986_EEPROM_STATUS_REG, LTC2986_EEPROM_STATUS_REG),
  1288. regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG),
  1289. regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG,
  1290. LTC2983_CHAN_ASSIGN_END_REG),
  1291. regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG,
  1292. LTC2983_CUST_SENS_TBL_END_REG),
  1293. };
  1294. static const struct regmap_access_table ltc2983_reg_table = {
  1295. .yes_ranges = ltc2983_reg_ranges,
  1296. .n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges),
  1297. };
  1298. /*
  1299. * The reg_bits are actually 12 but the device needs the first *complete*
  1300. * byte for the command (R/W).
  1301. */
  1302. static const struct regmap_config ltc2983_regmap_config = {
  1303. .reg_bits = 24,
  1304. .val_bits = 8,
  1305. .wr_table = &ltc2983_reg_table,
  1306. .rd_table = &ltc2983_reg_table,
  1307. .read_flag_mask = GENMASK(1, 0),
  1308. .write_flag_mask = BIT(1),
  1309. };
  1310. static const struct iio_info ltc2983_iio_info = {
  1311. .read_raw = ltc2983_read_raw,
  1312. .debugfs_reg_access = ltc2983_reg_access,
  1313. };
  1314. static int ltc2983_probe(struct spi_device *spi)
  1315. {
  1316. struct ltc2983_data *st;
  1317. struct iio_dev *indio_dev;
  1318. struct gpio_desc *gpio;
  1319. int ret;
  1320. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  1321. if (!indio_dev)
  1322. return -ENOMEM;
  1323. st = iio_priv(indio_dev);
  1324. st->info = spi_get_device_match_data(spi);
  1325. if (!st->info)
  1326. return -ENODEV;
  1327. st->regmap = devm_regmap_init_spi(spi, &ltc2983_regmap_config);
  1328. if (IS_ERR(st->regmap))
  1329. return dev_err_probe(&spi->dev, PTR_ERR(st->regmap),
  1330. "Failed to initialize regmap\n");
  1331. mutex_init(&st->lock);
  1332. init_completion(&st->completion);
  1333. st->spi = spi;
  1334. st->eeprom_key = cpu_to_be32(LTC2983_EEPROM_KEY);
  1335. spi_set_drvdata(spi, st);
  1336. ret = ltc2983_parse_fw(st);
  1337. if (ret)
  1338. return ret;
  1339. ret = devm_regulator_get_enable(&spi->dev, "vdd");
  1340. if (ret)
  1341. return ret;
  1342. gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH);
  1343. if (IS_ERR(gpio))
  1344. return PTR_ERR(gpio);
  1345. if (gpio) {
  1346. /* bring the device out of reset */
  1347. usleep_range(1000, 1200);
  1348. gpiod_set_value_cansleep(gpio, 0);
  1349. }
  1350. st->iio_chan = devm_kzalloc(&spi->dev,
  1351. st->iio_channels * sizeof(*st->iio_chan),
  1352. GFP_KERNEL);
  1353. if (!st->iio_chan)
  1354. return -ENOMEM;
  1355. ret = ltc2983_setup(st, true);
  1356. if (ret)
  1357. return ret;
  1358. ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
  1359. IRQF_TRIGGER_RISING, st->info->name, st);
  1360. if (ret)
  1361. return dev_err_probe(&spi->dev, ret,
  1362. "failed to request an irq\n");
  1363. if (st->info->has_eeprom) {
  1364. ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_WRITE_CMD,
  1365. LTC2983_EEPROM_WRITE_TIME_MS,
  1366. LTC2986_EEPROM_STATUS_REG,
  1367. LTC2983_EEPROM_STATUS_FAILURE_MASK);
  1368. if (ret)
  1369. return ret;
  1370. }
  1371. indio_dev->name = st->info->name;
  1372. indio_dev->num_channels = st->iio_channels;
  1373. indio_dev->channels = st->iio_chan;
  1374. indio_dev->modes = INDIO_DIRECT_MODE;
  1375. indio_dev->info = &ltc2983_iio_info;
  1376. return devm_iio_device_register(&spi->dev, indio_dev);
  1377. }
  1378. static int ltc2983_resume(struct device *dev)
  1379. {
  1380. struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
  1381. int dummy;
  1382. /* dummy read to bring the device out of sleep */
  1383. regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy);
  1384. /* we need to re-assign the channels */
  1385. return ltc2983_setup(st, false);
  1386. }
  1387. static int ltc2983_suspend(struct device *dev)
  1388. {
  1389. struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
  1390. return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
  1391. }
  1392. static DEFINE_SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend,
  1393. ltc2983_resume);
  1394. static const struct ltc2983_chip_info ltc2983_chip_info_data = {
  1395. .name = "ltc2983",
  1396. .max_channels_nr = 20,
  1397. };
  1398. static const struct ltc2983_chip_info ltc2984_chip_info_data = {
  1399. .name = "ltc2984",
  1400. .max_channels_nr = 20,
  1401. .has_eeprom = true,
  1402. };
  1403. static const struct ltc2983_chip_info ltc2986_chip_info_data = {
  1404. .name = "ltc2986",
  1405. .max_channels_nr = 10,
  1406. .has_temp = true,
  1407. .has_eeprom = true,
  1408. };
  1409. static const struct ltc2983_chip_info ltm2985_chip_info_data = {
  1410. .name = "ltm2985",
  1411. .max_channels_nr = 10,
  1412. .has_temp = true,
  1413. .has_eeprom = true,
  1414. };
  1415. static const struct spi_device_id ltc2983_id_table[] = {
  1416. { "ltc2983", (kernel_ulong_t)&ltc2983_chip_info_data },
  1417. { "ltc2984", (kernel_ulong_t)&ltc2984_chip_info_data },
  1418. { "ltc2986", (kernel_ulong_t)&ltc2986_chip_info_data },
  1419. { "ltm2985", (kernel_ulong_t)&ltm2985_chip_info_data },
  1420. {},
  1421. };
  1422. MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
  1423. static const struct of_device_id ltc2983_of_match[] = {
  1424. { .compatible = "adi,ltc2983", .data = &ltc2983_chip_info_data },
  1425. { .compatible = "adi,ltc2984", .data = &ltc2984_chip_info_data },
  1426. { .compatible = "adi,ltc2986", .data = &ltc2986_chip_info_data },
  1427. { .compatible = "adi,ltm2985", .data = &ltm2985_chip_info_data },
  1428. {},
  1429. };
  1430. MODULE_DEVICE_TABLE(of, ltc2983_of_match);
  1431. static struct spi_driver ltc2983_driver = {
  1432. .driver = {
  1433. .name = "ltc2983",
  1434. .of_match_table = ltc2983_of_match,
  1435. .pm = pm_sleep_ptr(&ltc2983_pm_ops),
  1436. },
  1437. .probe = ltc2983_probe,
  1438. .id_table = ltc2983_id_table,
  1439. };
  1440. module_spi_driver(ltc2983_driver);
  1441. MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
  1442. MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
  1443. MODULE_LICENSE("GPL");