ad74413r.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 Analog Devices, Inc.
  4. * Author: Cosmin Tanislav <cosmin.tanislav@analog.com>
  5. */
  6. #include <linux/unaligned.h>
  7. #include <linux/bitfield.h>
  8. #include <linux/crc8.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/iio/buffer.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/sysfs.h>
  15. #include <linux/iio/trigger.h>
  16. #include <linux/iio/trigger_consumer.h>
  17. #include <linux/iio/triggered_buffer.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/property.h>
  21. #include <linux/regmap.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/spi/spi.h>
  24. #include <dt-bindings/iio/addac/adi,ad74413r.h>
  25. #define AD74413R_CRC_POLYNOMIAL 0x7
  26. DECLARE_CRC8_TABLE(ad74413r_crc8_table);
  27. #define AD74413R_CHANNEL_MAX 4
  28. #define AD74413R_FRAME_SIZE 4
  29. struct ad74413r_chip_info {
  30. const char *name;
  31. bool hart_support;
  32. };
  33. struct ad74413r_channel_config {
  34. u32 func;
  35. u32 drive_strength;
  36. bool gpo_comparator;
  37. bool initialized;
  38. };
  39. struct ad74413r_channels {
  40. struct iio_chan_spec *channels;
  41. unsigned int num_channels;
  42. };
  43. struct ad74413r_state {
  44. struct ad74413r_channel_config channel_configs[AD74413R_CHANNEL_MAX];
  45. unsigned int gpo_gpio_offsets[AD74413R_CHANNEL_MAX];
  46. unsigned int comp_gpio_offsets[AD74413R_CHANNEL_MAX];
  47. struct gpio_chip gpo_gpiochip;
  48. struct gpio_chip comp_gpiochip;
  49. struct completion adc_data_completion;
  50. unsigned int num_gpo_gpios;
  51. unsigned int num_comparator_gpios;
  52. u32 sense_resistor_ohms;
  53. /*
  54. * Synchronize consecutive operations when doing a one-shot
  55. * conversion and when updating the ADC samples SPI message.
  56. */
  57. struct mutex lock;
  58. const struct ad74413r_chip_info *chip_info;
  59. struct spi_device *spi;
  60. struct regulator *refin_reg;
  61. struct regmap *regmap;
  62. struct device *dev;
  63. struct iio_trigger *trig;
  64. struct gpio_desc *reset_gpio;
  65. size_t adc_active_channels;
  66. struct spi_message adc_samples_msg;
  67. struct spi_transfer adc_samples_xfer[AD74413R_CHANNEL_MAX + 1];
  68. /*
  69. * DMA (thus cache coherency maintenance) may require the
  70. * transfer buffers to live in their own cache lines.
  71. */
  72. struct {
  73. u8 rx_buf[AD74413R_FRAME_SIZE * AD74413R_CHANNEL_MAX];
  74. s64 timestamp;
  75. } adc_samples_buf __aligned(IIO_DMA_MINALIGN);
  76. u8 adc_samples_tx_buf[AD74413R_FRAME_SIZE * AD74413R_CHANNEL_MAX];
  77. u8 reg_tx_buf[AD74413R_FRAME_SIZE];
  78. u8 reg_rx_buf[AD74413R_FRAME_SIZE];
  79. };
  80. #define AD74413R_REG_NOP 0x00
  81. #define AD74413R_REG_CH_FUNC_SETUP_X(x) (0x01 + (x))
  82. #define AD74413R_CH_FUNC_SETUP_MASK GENMASK(3, 0)
  83. #define AD74413R_REG_ADC_CONFIG_X(x) (0x05 + (x))
  84. #define AD74413R_ADC_CONFIG_RANGE_MASK GENMASK(7, 5)
  85. #define AD74413R_ADC_CONFIG_REJECTION_MASK GENMASK(4, 3)
  86. #define AD74413R_ADC_CONFIG_CH_200K_TO_GND BIT(2)
  87. #define AD74413R_ADC_RANGE_10V 0b000
  88. #define AD74413R_ADC_RANGE_2P5V_EXT_POW 0b001
  89. #define AD74413R_ADC_RANGE_2P5V_INT_POW 0b010
  90. #define AD74413R_ADC_RANGE_5V_BI_DIR 0b011
  91. #define AD74413R_ADC_REJECTION_50_60 0b00
  92. #define AD74413R_ADC_REJECTION_NONE 0b01
  93. #define AD74413R_ADC_REJECTION_50_60_HART 0b10
  94. #define AD74413R_ADC_REJECTION_HART 0b11
  95. #define AD74413R_REG_DIN_CONFIG_X(x) (0x09 + (x))
  96. #define AD74413R_DIN_DEBOUNCE_MASK GENMASK(4, 0)
  97. #define AD74413R_DIN_DEBOUNCE_LEN BIT(5)
  98. #define AD74413R_DIN_SINK_MASK GENMASK(9, 6)
  99. #define AD74413R_REG_DAC_CODE_X(x) (0x16 + (x))
  100. #define AD74413R_DAC_CODE_MAX GENMASK(12, 0)
  101. #define AD74413R_DAC_VOLTAGE_MAX 11000
  102. #define AD74413R_REG_GPO_PAR_DATA 0x0d
  103. #define AD74413R_REG_GPO_CONFIG_X(x) (0x0e + (x))
  104. #define AD74413R_GPO_CONFIG_DATA_MASK BIT(3)
  105. #define AD74413R_GPO_CONFIG_SELECT_MASK GENMASK(2, 0)
  106. #define AD74413R_GPO_CONFIG_100K_PULL_DOWN 0b000
  107. #define AD74413R_GPO_CONFIG_LOGIC 0b001
  108. #define AD74413R_GPO_CONFIG_LOGIC_PARALLEL 0b010
  109. #define AD74413R_GPO_CONFIG_COMPARATOR 0b011
  110. #define AD74413R_GPO_CONFIG_HIGH_IMPEDANCE 0b100
  111. #define AD74413R_REG_ADC_CONV_CTRL 0x23
  112. #define AD74413R_CONV_SEQ_MASK GENMASK(9, 8)
  113. #define AD74413R_CONV_SEQ_ON 0b00
  114. #define AD74413R_CONV_SEQ_SINGLE 0b01
  115. #define AD74413R_CONV_SEQ_CONTINUOUS 0b10
  116. #define AD74413R_CONV_SEQ_OFF 0b11
  117. #define AD74413R_CH_EN_MASK(x) BIT(x)
  118. #define AD74413R_REG_DIN_COMP_OUT 0x25
  119. #define AD74413R_REG_ADC_RESULT_X(x) (0x26 + (x))
  120. #define AD74413R_ADC_RESULT_MAX GENMASK(15, 0)
  121. #define AD74413R_REG_READ_SELECT 0x41
  122. #define AD74413R_REG_CMD_KEY 0x44
  123. #define AD74413R_CMD_KEY_LDAC 0x953a
  124. #define AD74413R_CMD_KEY_RESET1 0x15fa
  125. #define AD74413R_CMD_KEY_RESET2 0xaf51
  126. static const int ad74413r_adc_sampling_rates[] = {
  127. 20, 4800,
  128. };
  129. static const int ad74413r_adc_sampling_rates_hart[] = {
  130. 10, 20, 1200, 4800,
  131. };
  132. static int ad74413r_crc(u8 *buf)
  133. {
  134. return crc8(ad74413r_crc8_table, buf, 3, 0);
  135. }
  136. static void ad74413r_format_reg_write(u8 reg, u16 val, u8 *buf)
  137. {
  138. buf[0] = reg;
  139. put_unaligned_be16(val, &buf[1]);
  140. buf[3] = ad74413r_crc(buf);
  141. }
  142. static int ad74413r_reg_write(void *context, unsigned int reg, unsigned int val)
  143. {
  144. struct ad74413r_state *st = context;
  145. ad74413r_format_reg_write(reg, val, st->reg_tx_buf);
  146. return spi_write(st->spi, st->reg_tx_buf, AD74413R_FRAME_SIZE);
  147. }
  148. static int ad74413r_crc_check(struct ad74413r_state *st, u8 *buf)
  149. {
  150. u8 expected_crc = ad74413r_crc(buf);
  151. if (buf[3] != expected_crc) {
  152. dev_err(st->dev, "Bad CRC %02x for %02x%02x%02x\n",
  153. buf[3], buf[0], buf[1], buf[2]);
  154. return -EINVAL;
  155. }
  156. return 0;
  157. }
  158. static int ad74413r_reg_read(void *context, unsigned int reg, unsigned int *val)
  159. {
  160. struct ad74413r_state *st = context;
  161. struct spi_transfer reg_read_xfer[] = {
  162. {
  163. .tx_buf = st->reg_tx_buf,
  164. .len = AD74413R_FRAME_SIZE,
  165. .cs_change = 1,
  166. },
  167. {
  168. .rx_buf = st->reg_rx_buf,
  169. .len = AD74413R_FRAME_SIZE,
  170. },
  171. };
  172. int ret;
  173. ad74413r_format_reg_write(AD74413R_REG_READ_SELECT, reg,
  174. st->reg_tx_buf);
  175. ret = spi_sync_transfer(st->spi, reg_read_xfer,
  176. ARRAY_SIZE(reg_read_xfer));
  177. if (ret)
  178. return ret;
  179. ret = ad74413r_crc_check(st, st->reg_rx_buf);
  180. if (ret)
  181. return ret;
  182. *val = get_unaligned_be16(&st->reg_rx_buf[1]);
  183. return 0;
  184. }
  185. static const struct regmap_config ad74413r_regmap_config = {
  186. .reg_bits = 8,
  187. .val_bits = 16,
  188. .reg_read = ad74413r_reg_read,
  189. .reg_write = ad74413r_reg_write,
  190. };
  191. static int ad74413r_set_gpo_config(struct ad74413r_state *st,
  192. unsigned int offset, u8 mode)
  193. {
  194. return regmap_update_bits(st->regmap, AD74413R_REG_GPO_CONFIG_X(offset),
  195. AD74413R_GPO_CONFIG_SELECT_MASK, mode);
  196. }
  197. static const unsigned int ad74413r_debounce_map[AD74413R_DIN_DEBOUNCE_LEN] = {
  198. 0, 13, 18, 24, 32, 42, 56, 75,
  199. 100, 130, 180, 240, 320, 420, 560, 750,
  200. 1000, 1300, 1800, 2400, 3200, 4200, 5600, 7500,
  201. 10000, 13000, 18000, 24000, 32000, 42000, 56000, 75000,
  202. };
  203. static int ad74413r_set_comp_debounce(struct ad74413r_state *st,
  204. unsigned int offset,
  205. unsigned int debounce)
  206. {
  207. unsigned int val = AD74413R_DIN_DEBOUNCE_LEN - 1;
  208. unsigned int i;
  209. for (i = 0; i < AD74413R_DIN_DEBOUNCE_LEN; i++)
  210. if (debounce <= ad74413r_debounce_map[i]) {
  211. val = i;
  212. break;
  213. }
  214. return regmap_update_bits(st->regmap,
  215. AD74413R_REG_DIN_CONFIG_X(offset),
  216. AD74413R_DIN_DEBOUNCE_MASK,
  217. val);
  218. }
  219. static int ad74413r_set_comp_drive_strength(struct ad74413r_state *st,
  220. unsigned int offset,
  221. unsigned int strength)
  222. {
  223. strength = min(strength, 1800U);
  224. return regmap_update_bits(st->regmap, AD74413R_REG_DIN_CONFIG_X(offset),
  225. AD74413R_DIN_SINK_MASK,
  226. FIELD_PREP(AD74413R_DIN_SINK_MASK, strength / 120));
  227. }
  228. static void ad74413r_gpio_set(struct gpio_chip *chip,
  229. unsigned int offset, int val)
  230. {
  231. struct ad74413r_state *st = gpiochip_get_data(chip);
  232. unsigned int real_offset = st->gpo_gpio_offsets[offset];
  233. int ret;
  234. ret = ad74413r_set_gpo_config(st, real_offset,
  235. AD74413R_GPO_CONFIG_LOGIC);
  236. if (ret)
  237. return;
  238. regmap_update_bits(st->regmap, AD74413R_REG_GPO_CONFIG_X(real_offset),
  239. AD74413R_GPO_CONFIG_DATA_MASK,
  240. val ? AD74413R_GPO_CONFIG_DATA_MASK : 0);
  241. }
  242. static void ad74413r_gpio_set_multiple(struct gpio_chip *chip,
  243. unsigned long *mask,
  244. unsigned long *bits)
  245. {
  246. struct ad74413r_state *st = gpiochip_get_data(chip);
  247. unsigned long real_mask = 0;
  248. unsigned long real_bits = 0;
  249. unsigned int offset;
  250. int ret;
  251. for_each_set_bit(offset, mask, chip->ngpio) {
  252. unsigned int real_offset = st->gpo_gpio_offsets[offset];
  253. ret = ad74413r_set_gpo_config(st, real_offset,
  254. AD74413R_GPO_CONFIG_LOGIC_PARALLEL);
  255. if (ret)
  256. return;
  257. real_mask |= BIT(real_offset);
  258. if (*bits & offset)
  259. real_bits |= BIT(real_offset);
  260. }
  261. regmap_update_bits(st->regmap, AD74413R_REG_GPO_PAR_DATA,
  262. real_mask, real_bits);
  263. }
  264. static int ad74413r_gpio_get(struct gpio_chip *chip, unsigned int offset)
  265. {
  266. struct ad74413r_state *st = gpiochip_get_data(chip);
  267. unsigned int real_offset = st->comp_gpio_offsets[offset];
  268. unsigned int status;
  269. int ret;
  270. ret = regmap_read(st->regmap, AD74413R_REG_DIN_COMP_OUT, &status);
  271. if (ret)
  272. return ret;
  273. status &= BIT(real_offset);
  274. return status ? 1 : 0;
  275. }
  276. static int ad74413r_gpio_get_multiple(struct gpio_chip *chip,
  277. unsigned long *mask,
  278. unsigned long *bits)
  279. {
  280. struct ad74413r_state *st = gpiochip_get_data(chip);
  281. unsigned int offset;
  282. unsigned int val;
  283. int ret;
  284. ret = regmap_read(st->regmap, AD74413R_REG_DIN_COMP_OUT, &val);
  285. if (ret)
  286. return ret;
  287. for_each_set_bit(offset, mask, chip->ngpio) {
  288. unsigned int real_offset = st->comp_gpio_offsets[offset];
  289. __assign_bit(offset, bits, val & BIT(real_offset));
  290. }
  291. return ret;
  292. }
  293. static int ad74413r_gpio_get_gpo_direction(struct gpio_chip *chip,
  294. unsigned int offset)
  295. {
  296. return GPIO_LINE_DIRECTION_OUT;
  297. }
  298. static int ad74413r_gpio_get_comp_direction(struct gpio_chip *chip,
  299. unsigned int offset)
  300. {
  301. return GPIO_LINE_DIRECTION_IN;
  302. }
  303. static int ad74413r_gpio_set_gpo_config(struct gpio_chip *chip,
  304. unsigned int offset,
  305. unsigned long config)
  306. {
  307. struct ad74413r_state *st = gpiochip_get_data(chip);
  308. unsigned int real_offset = st->gpo_gpio_offsets[offset];
  309. switch (pinconf_to_config_param(config)) {
  310. case PIN_CONFIG_BIAS_PULL_DOWN:
  311. return ad74413r_set_gpo_config(st, real_offset,
  312. AD74413R_GPO_CONFIG_100K_PULL_DOWN);
  313. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  314. return ad74413r_set_gpo_config(st, real_offset,
  315. AD74413R_GPO_CONFIG_HIGH_IMPEDANCE);
  316. default:
  317. return -ENOTSUPP;
  318. }
  319. }
  320. static int ad74413r_gpio_set_comp_config(struct gpio_chip *chip,
  321. unsigned int offset,
  322. unsigned long config)
  323. {
  324. struct ad74413r_state *st = gpiochip_get_data(chip);
  325. unsigned int real_offset = st->comp_gpio_offsets[offset];
  326. switch (pinconf_to_config_param(config)) {
  327. case PIN_CONFIG_INPUT_DEBOUNCE:
  328. return ad74413r_set_comp_debounce(st, real_offset,
  329. pinconf_to_config_argument(config));
  330. default:
  331. return -ENOTSUPP;
  332. }
  333. }
  334. static int ad74413r_reset(struct ad74413r_state *st)
  335. {
  336. int ret;
  337. if (st->reset_gpio) {
  338. gpiod_set_value_cansleep(st->reset_gpio, 1);
  339. fsleep(50);
  340. gpiod_set_value_cansleep(st->reset_gpio, 0);
  341. return 0;
  342. }
  343. ret = regmap_write(st->regmap, AD74413R_REG_CMD_KEY,
  344. AD74413R_CMD_KEY_RESET1);
  345. if (ret)
  346. return ret;
  347. return regmap_write(st->regmap, AD74413R_REG_CMD_KEY,
  348. AD74413R_CMD_KEY_RESET2);
  349. }
  350. static int ad74413r_set_channel_dac_code(struct ad74413r_state *st,
  351. unsigned int channel, int dac_code)
  352. {
  353. struct reg_sequence reg_seq[2] = {
  354. { AD74413R_REG_DAC_CODE_X(channel), dac_code },
  355. { AD74413R_REG_CMD_KEY, AD74413R_CMD_KEY_LDAC },
  356. };
  357. return regmap_multi_reg_write(st->regmap, reg_seq, 2);
  358. }
  359. static int ad74413r_set_channel_function(struct ad74413r_state *st,
  360. unsigned int channel, u8 func)
  361. {
  362. int ret;
  363. ret = regmap_update_bits(st->regmap,
  364. AD74413R_REG_CH_FUNC_SETUP_X(channel),
  365. AD74413R_CH_FUNC_SETUP_MASK,
  366. CH_FUNC_HIGH_IMPEDANCE);
  367. if (ret)
  368. return ret;
  369. /* Set DAC code to 0 prior to changing channel function */
  370. ret = ad74413r_set_channel_dac_code(st, channel, 0);
  371. if (ret)
  372. return ret;
  373. /* Delay required before transition to new desired mode */
  374. usleep_range(130, 150);
  375. ret = regmap_update_bits(st->regmap,
  376. AD74413R_REG_CH_FUNC_SETUP_X(channel),
  377. AD74413R_CH_FUNC_SETUP_MASK, func);
  378. if (ret)
  379. return ret;
  380. /* Delay required before updating the new DAC code */
  381. usleep_range(150, 170);
  382. if (func == CH_FUNC_CURRENT_INPUT_LOOP_POWER)
  383. ret = regmap_set_bits(st->regmap,
  384. AD74413R_REG_ADC_CONFIG_X(channel),
  385. AD74413R_ADC_CONFIG_CH_200K_TO_GND);
  386. return ret;
  387. }
  388. static int ad74413r_set_adc_conv_seq(struct ad74413r_state *st,
  389. unsigned int status)
  390. {
  391. int ret;
  392. /*
  393. * These bits do not clear when a conversion completes.
  394. * To enable a subsequent conversion, repeat the write.
  395. */
  396. ret = regmap_write_bits(st->regmap, AD74413R_REG_ADC_CONV_CTRL,
  397. AD74413R_CONV_SEQ_MASK,
  398. FIELD_PREP(AD74413R_CONV_SEQ_MASK, status));
  399. if (ret)
  400. return ret;
  401. /*
  402. * Wait 100us before starting conversions.
  403. */
  404. usleep_range(100, 120);
  405. return 0;
  406. }
  407. static int ad74413r_set_adc_channel_enable(struct ad74413r_state *st,
  408. unsigned int channel,
  409. bool status)
  410. {
  411. return regmap_update_bits(st->regmap, AD74413R_REG_ADC_CONV_CTRL,
  412. AD74413R_CH_EN_MASK(channel),
  413. status ? AD74413R_CH_EN_MASK(channel) : 0);
  414. }
  415. static int ad74413r_get_adc_range(struct ad74413r_state *st,
  416. unsigned int channel,
  417. unsigned int *val)
  418. {
  419. int ret;
  420. ret = regmap_read(st->regmap, AD74413R_REG_ADC_CONFIG_X(channel), val);
  421. if (ret)
  422. return ret;
  423. *val = FIELD_GET(AD74413R_ADC_CONFIG_RANGE_MASK, *val);
  424. return 0;
  425. }
  426. static int ad74413r_get_adc_rejection(struct ad74413r_state *st,
  427. unsigned int channel,
  428. unsigned int *val)
  429. {
  430. int ret;
  431. ret = regmap_read(st->regmap, AD74413R_REG_ADC_CONFIG_X(channel), val);
  432. if (ret)
  433. return ret;
  434. *val = FIELD_GET(AD74413R_ADC_CONFIG_REJECTION_MASK, *val);
  435. return 0;
  436. }
  437. static int ad74413r_set_adc_rejection(struct ad74413r_state *st,
  438. unsigned int channel,
  439. unsigned int val)
  440. {
  441. return regmap_update_bits(st->regmap,
  442. AD74413R_REG_ADC_CONFIG_X(channel),
  443. AD74413R_ADC_CONFIG_REJECTION_MASK,
  444. FIELD_PREP(AD74413R_ADC_CONFIG_REJECTION_MASK,
  445. val));
  446. }
  447. static int ad74413r_rejection_to_rate(struct ad74413r_state *st,
  448. unsigned int rej, int *val)
  449. {
  450. switch (rej) {
  451. case AD74413R_ADC_REJECTION_50_60:
  452. *val = 20;
  453. return 0;
  454. case AD74413R_ADC_REJECTION_NONE:
  455. *val = 4800;
  456. return 0;
  457. case AD74413R_ADC_REJECTION_50_60_HART:
  458. *val = 10;
  459. return 0;
  460. case AD74413R_ADC_REJECTION_HART:
  461. *val = 1200;
  462. return 0;
  463. default:
  464. dev_err(st->dev, "ADC rejection invalid\n");
  465. return -EINVAL;
  466. }
  467. }
  468. static int ad74413r_rate_to_rejection(struct ad74413r_state *st,
  469. int rate, unsigned int *val)
  470. {
  471. switch (rate) {
  472. case 20:
  473. *val = AD74413R_ADC_REJECTION_50_60;
  474. return 0;
  475. case 4800:
  476. *val = AD74413R_ADC_REJECTION_NONE;
  477. return 0;
  478. case 10:
  479. *val = AD74413R_ADC_REJECTION_50_60_HART;
  480. return 0;
  481. case 1200:
  482. *val = AD74413R_ADC_REJECTION_HART;
  483. return 0;
  484. default:
  485. dev_err(st->dev, "ADC rate invalid\n");
  486. return -EINVAL;
  487. }
  488. }
  489. static int ad74413r_range_to_voltage_range(struct ad74413r_state *st,
  490. unsigned int range, int *val)
  491. {
  492. switch (range) {
  493. case AD74413R_ADC_RANGE_10V:
  494. *val = 10000;
  495. return 0;
  496. case AD74413R_ADC_RANGE_2P5V_EXT_POW:
  497. case AD74413R_ADC_RANGE_2P5V_INT_POW:
  498. *val = 2500;
  499. return 0;
  500. case AD74413R_ADC_RANGE_5V_BI_DIR:
  501. *val = 5000;
  502. return 0;
  503. default:
  504. dev_err(st->dev, "ADC range invalid\n");
  505. return -EINVAL;
  506. }
  507. }
  508. static int ad74413r_range_to_voltage_offset(struct ad74413r_state *st,
  509. unsigned int range, int *val)
  510. {
  511. switch (range) {
  512. case AD74413R_ADC_RANGE_10V:
  513. case AD74413R_ADC_RANGE_2P5V_EXT_POW:
  514. *val = 0;
  515. return 0;
  516. case AD74413R_ADC_RANGE_2P5V_INT_POW:
  517. case AD74413R_ADC_RANGE_5V_BI_DIR:
  518. *val = -2500;
  519. return 0;
  520. default:
  521. dev_err(st->dev, "ADC range invalid\n");
  522. return -EINVAL;
  523. }
  524. }
  525. static int ad74413r_range_to_voltage_offset_raw(struct ad74413r_state *st,
  526. unsigned int range, int *val)
  527. {
  528. switch (range) {
  529. case AD74413R_ADC_RANGE_10V:
  530. case AD74413R_ADC_RANGE_2P5V_EXT_POW:
  531. *val = 0;
  532. return 0;
  533. case AD74413R_ADC_RANGE_2P5V_INT_POW:
  534. *val = -((int)AD74413R_ADC_RESULT_MAX);
  535. return 0;
  536. case AD74413R_ADC_RANGE_5V_BI_DIR:
  537. *val = -((int)AD74413R_ADC_RESULT_MAX / 2);
  538. return 0;
  539. default:
  540. dev_err(st->dev, "ADC range invalid\n");
  541. return -EINVAL;
  542. }
  543. }
  544. static int ad74413r_get_output_voltage_scale(struct ad74413r_state *st,
  545. int *val, int *val2)
  546. {
  547. *val = AD74413R_DAC_VOLTAGE_MAX;
  548. *val2 = AD74413R_DAC_CODE_MAX;
  549. return IIO_VAL_FRACTIONAL;
  550. }
  551. static int ad74413r_get_output_current_scale(struct ad74413r_state *st,
  552. int *val, int *val2)
  553. {
  554. *val = regulator_get_voltage(st->refin_reg);
  555. *val2 = st->sense_resistor_ohms * AD74413R_DAC_CODE_MAX * 1000;
  556. return IIO_VAL_FRACTIONAL;
  557. }
  558. static int ad74413r_get_input_voltage_scale(struct ad74413r_state *st,
  559. unsigned int channel,
  560. int *val, int *val2)
  561. {
  562. unsigned int range;
  563. int ret;
  564. ret = ad74413r_get_adc_range(st, channel, &range);
  565. if (ret)
  566. return ret;
  567. ret = ad74413r_range_to_voltage_range(st, range, val);
  568. if (ret)
  569. return ret;
  570. *val2 = AD74413R_ADC_RESULT_MAX;
  571. return IIO_VAL_FRACTIONAL;
  572. }
  573. static int ad74413r_get_input_voltage_offset(struct ad74413r_state *st,
  574. unsigned int channel, int *val)
  575. {
  576. unsigned int range;
  577. int ret;
  578. ret = ad74413r_get_adc_range(st, channel, &range);
  579. if (ret)
  580. return ret;
  581. ret = ad74413r_range_to_voltage_offset_raw(st, range, val);
  582. if (ret)
  583. return ret;
  584. return IIO_VAL_INT;
  585. }
  586. static int ad74413r_get_input_current_scale(struct ad74413r_state *st,
  587. unsigned int channel, int *val,
  588. int *val2)
  589. {
  590. unsigned int range;
  591. int ret;
  592. ret = ad74413r_get_adc_range(st, channel, &range);
  593. if (ret)
  594. return ret;
  595. ret = ad74413r_range_to_voltage_range(st, range, val);
  596. if (ret)
  597. return ret;
  598. *val2 = AD74413R_ADC_RESULT_MAX * st->sense_resistor_ohms;
  599. return IIO_VAL_FRACTIONAL;
  600. }
  601. static int ad74413r_get_input_current_offset(struct ad74413r_state *st,
  602. unsigned int channel, int *val)
  603. {
  604. unsigned int range;
  605. int voltage_range;
  606. int voltage_offset;
  607. int ret;
  608. ret = ad74413r_get_adc_range(st, channel, &range);
  609. if (ret)
  610. return ret;
  611. ret = ad74413r_range_to_voltage_range(st, range, &voltage_range);
  612. if (ret)
  613. return ret;
  614. ret = ad74413r_range_to_voltage_offset(st, range, &voltage_offset);
  615. if (ret)
  616. return ret;
  617. *val = voltage_offset * (int)AD74413R_ADC_RESULT_MAX / voltage_range;
  618. return IIO_VAL_INT;
  619. }
  620. static int ad74413r_get_adc_rate(struct ad74413r_state *st,
  621. unsigned int channel, int *val)
  622. {
  623. unsigned int rejection;
  624. int ret;
  625. ret = ad74413r_get_adc_rejection(st, channel, &rejection);
  626. if (ret)
  627. return ret;
  628. ret = ad74413r_rejection_to_rate(st, rejection, val);
  629. if (ret)
  630. return ret;
  631. return IIO_VAL_INT;
  632. }
  633. static int ad74413r_set_adc_rate(struct ad74413r_state *st,
  634. unsigned int channel, int val)
  635. {
  636. unsigned int rejection;
  637. int ret;
  638. ret = ad74413r_rate_to_rejection(st, val, &rejection);
  639. if (ret)
  640. return ret;
  641. return ad74413r_set_adc_rejection(st, channel, rejection);
  642. }
  643. static irqreturn_t ad74413r_trigger_handler(int irq, void *p)
  644. {
  645. struct iio_poll_func *pf = p;
  646. struct iio_dev *indio_dev = pf->indio_dev;
  647. struct ad74413r_state *st = iio_priv(indio_dev);
  648. u8 *rx_buf = st->adc_samples_buf.rx_buf;
  649. unsigned int i;
  650. int ret;
  651. ret = spi_sync(st->spi, &st->adc_samples_msg);
  652. if (ret)
  653. goto out;
  654. for (i = 0; i < st->adc_active_channels; i++)
  655. ad74413r_crc_check(st, &rx_buf[i * AD74413R_FRAME_SIZE]);
  656. iio_push_to_buffers_with_timestamp(indio_dev, &st->adc_samples_buf,
  657. iio_get_time_ns(indio_dev));
  658. out:
  659. iio_trigger_notify_done(indio_dev->trig);
  660. return IRQ_HANDLED;
  661. }
  662. static irqreturn_t ad74413r_adc_data_interrupt(int irq, void *data)
  663. {
  664. struct iio_dev *indio_dev = data;
  665. struct ad74413r_state *st = iio_priv(indio_dev);
  666. if (iio_buffer_enabled(indio_dev))
  667. iio_trigger_poll(st->trig);
  668. else
  669. complete(&st->adc_data_completion);
  670. return IRQ_HANDLED;
  671. }
  672. static int _ad74413r_get_single_adc_result(struct ad74413r_state *st,
  673. unsigned int channel, int *val)
  674. {
  675. unsigned int uval;
  676. int ret;
  677. reinit_completion(&st->adc_data_completion);
  678. ret = ad74413r_set_adc_channel_enable(st, channel, true);
  679. if (ret)
  680. return ret;
  681. ret = ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_SINGLE);
  682. if (ret)
  683. return ret;
  684. ret = wait_for_completion_timeout(&st->adc_data_completion,
  685. msecs_to_jiffies(1000));
  686. if (!ret) {
  687. ret = -ETIMEDOUT;
  688. return ret;
  689. }
  690. ret = regmap_read(st->regmap, AD74413R_REG_ADC_RESULT_X(channel),
  691. &uval);
  692. if (ret)
  693. return ret;
  694. ret = ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_OFF);
  695. if (ret)
  696. return ret;
  697. ret = ad74413r_set_adc_channel_enable(st, channel, false);
  698. if (ret)
  699. return ret;
  700. *val = uval;
  701. return IIO_VAL_INT;
  702. }
  703. static int ad74413r_get_single_adc_result(struct iio_dev *indio_dev,
  704. unsigned int channel, int *val)
  705. {
  706. struct ad74413r_state *st = iio_priv(indio_dev);
  707. int ret;
  708. ret = iio_device_claim_direct_mode(indio_dev);
  709. if (ret)
  710. return ret;
  711. mutex_lock(&st->lock);
  712. ret = _ad74413r_get_single_adc_result(st, channel, val);
  713. mutex_unlock(&st->lock);
  714. iio_device_release_direct_mode(indio_dev);
  715. return ret;
  716. }
  717. static void ad74413r_adc_to_resistance_result(int adc_result, int *val)
  718. {
  719. if (adc_result == AD74413R_ADC_RESULT_MAX)
  720. adc_result = AD74413R_ADC_RESULT_MAX - 1;
  721. *val = DIV_ROUND_CLOSEST(adc_result * 2100,
  722. AD74413R_ADC_RESULT_MAX - adc_result);
  723. }
  724. static int ad74413r_update_scan_mode(struct iio_dev *indio_dev,
  725. const unsigned long *active_scan_mask)
  726. {
  727. struct ad74413r_state *st = iio_priv(indio_dev);
  728. struct spi_transfer *xfer = st->adc_samples_xfer;
  729. u8 *rx_buf = st->adc_samples_buf.rx_buf;
  730. u8 *tx_buf = st->adc_samples_tx_buf;
  731. unsigned int channel;
  732. int ret = -EINVAL;
  733. mutex_lock(&st->lock);
  734. spi_message_init(&st->adc_samples_msg);
  735. st->adc_active_channels = 0;
  736. for_each_clear_bit(channel, active_scan_mask, AD74413R_CHANNEL_MAX) {
  737. ret = ad74413r_set_adc_channel_enable(st, channel, false);
  738. if (ret)
  739. goto out;
  740. }
  741. if (*active_scan_mask == 0)
  742. goto out;
  743. /*
  744. * The read select register is used to select which register's value
  745. * will be sent by the slave on the next SPI frame.
  746. *
  747. * Create an SPI message that, on each step, writes to the read select
  748. * register to select the ADC result of the next enabled channel, and
  749. * reads the ADC result of the previous enabled channel.
  750. *
  751. * Example:
  752. * W: [WCH1] [WCH2] [WCH2] [WCH3] [ ]
  753. * R: [ ] [RCH1] [RCH2] [RCH3] [RCH4]
  754. */
  755. for_each_set_bit(channel, active_scan_mask, AD74413R_CHANNEL_MAX) {
  756. ret = ad74413r_set_adc_channel_enable(st, channel, true);
  757. if (ret)
  758. goto out;
  759. st->adc_active_channels++;
  760. if (xfer == st->adc_samples_xfer)
  761. xfer->rx_buf = NULL;
  762. else
  763. xfer->rx_buf = rx_buf;
  764. xfer->tx_buf = tx_buf;
  765. xfer->len = AD74413R_FRAME_SIZE;
  766. xfer->cs_change = 1;
  767. ad74413r_format_reg_write(AD74413R_REG_READ_SELECT,
  768. AD74413R_REG_ADC_RESULT_X(channel),
  769. tx_buf);
  770. spi_message_add_tail(xfer, &st->adc_samples_msg);
  771. tx_buf += AD74413R_FRAME_SIZE;
  772. if (xfer != st->adc_samples_xfer)
  773. rx_buf += AD74413R_FRAME_SIZE;
  774. xfer++;
  775. }
  776. xfer->rx_buf = rx_buf;
  777. xfer->tx_buf = NULL;
  778. xfer->len = AD74413R_FRAME_SIZE;
  779. xfer->cs_change = 0;
  780. spi_message_add_tail(xfer, &st->adc_samples_msg);
  781. out:
  782. mutex_unlock(&st->lock);
  783. return ret;
  784. }
  785. static int ad74413r_buffer_postenable(struct iio_dev *indio_dev)
  786. {
  787. struct ad74413r_state *st = iio_priv(indio_dev);
  788. return ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_CONTINUOUS);
  789. }
  790. static int ad74413r_buffer_predisable(struct iio_dev *indio_dev)
  791. {
  792. struct ad74413r_state *st = iio_priv(indio_dev);
  793. return ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_OFF);
  794. }
  795. static int ad74413r_read_raw(struct iio_dev *indio_dev,
  796. struct iio_chan_spec const *chan,
  797. int *val, int *val2, long info)
  798. {
  799. struct ad74413r_state *st = iio_priv(indio_dev);
  800. switch (info) {
  801. case IIO_CHAN_INFO_SCALE:
  802. switch (chan->type) {
  803. case IIO_VOLTAGE:
  804. if (chan->output)
  805. return ad74413r_get_output_voltage_scale(st,
  806. val, val2);
  807. else
  808. return ad74413r_get_input_voltage_scale(st,
  809. chan->channel, val, val2);
  810. case IIO_CURRENT:
  811. if (chan->output)
  812. return ad74413r_get_output_current_scale(st,
  813. val, val2);
  814. else
  815. return ad74413r_get_input_current_scale(st,
  816. chan->channel, val, val2);
  817. default:
  818. return -EINVAL;
  819. }
  820. case IIO_CHAN_INFO_OFFSET:
  821. switch (chan->type) {
  822. case IIO_VOLTAGE:
  823. return ad74413r_get_input_voltage_offset(st,
  824. chan->channel, val);
  825. case IIO_CURRENT:
  826. return ad74413r_get_input_current_offset(st,
  827. chan->channel, val);
  828. default:
  829. return -EINVAL;
  830. }
  831. case IIO_CHAN_INFO_RAW:
  832. if (chan->output)
  833. return -EINVAL;
  834. return ad74413r_get_single_adc_result(indio_dev, chan->channel,
  835. val);
  836. case IIO_CHAN_INFO_PROCESSED: {
  837. int ret;
  838. ret = ad74413r_get_single_adc_result(indio_dev, chan->channel,
  839. val);
  840. if (ret < 0)
  841. return ret;
  842. ad74413r_adc_to_resistance_result(*val, val);
  843. return ret;
  844. }
  845. case IIO_CHAN_INFO_SAMP_FREQ:
  846. return ad74413r_get_adc_rate(st, chan->channel, val);
  847. default:
  848. return -EINVAL;
  849. }
  850. }
  851. static int ad74413r_write_raw(struct iio_dev *indio_dev,
  852. struct iio_chan_spec const *chan,
  853. int val, int val2, long info)
  854. {
  855. struct ad74413r_state *st = iio_priv(indio_dev);
  856. switch (info) {
  857. case IIO_CHAN_INFO_RAW:
  858. if (!chan->output)
  859. return -EINVAL;
  860. if (val < 0 || val > AD74413R_DAC_CODE_MAX) {
  861. dev_err(st->dev, "Invalid DAC code\n");
  862. return -EINVAL;
  863. }
  864. return ad74413r_set_channel_dac_code(st, chan->channel, val);
  865. case IIO_CHAN_INFO_SAMP_FREQ:
  866. return ad74413r_set_adc_rate(st, chan->channel, val);
  867. default:
  868. return -EINVAL;
  869. }
  870. }
  871. static int ad74413r_read_avail(struct iio_dev *indio_dev,
  872. struct iio_chan_spec const *chan,
  873. const int **vals, int *type, int *length,
  874. long info)
  875. {
  876. struct ad74413r_state *st = iio_priv(indio_dev);
  877. switch (info) {
  878. case IIO_CHAN_INFO_SAMP_FREQ:
  879. if (st->chip_info->hart_support) {
  880. *vals = ad74413r_adc_sampling_rates_hart;
  881. *length = ARRAY_SIZE(ad74413r_adc_sampling_rates_hart);
  882. } else {
  883. *vals = ad74413r_adc_sampling_rates;
  884. *length = ARRAY_SIZE(ad74413r_adc_sampling_rates);
  885. }
  886. *type = IIO_VAL_INT;
  887. return IIO_AVAIL_LIST;
  888. default:
  889. return -EINVAL;
  890. }
  891. }
  892. static const struct iio_buffer_setup_ops ad74413r_buffer_ops = {
  893. .postenable = &ad74413r_buffer_postenable,
  894. .predisable = &ad74413r_buffer_predisable,
  895. };
  896. static const struct iio_trigger_ops ad74413r_trigger_ops = {
  897. .validate_device = iio_trigger_validate_own_device,
  898. };
  899. static const struct iio_info ad74413r_info = {
  900. .read_raw = &ad74413r_read_raw,
  901. .write_raw = &ad74413r_write_raw,
  902. .read_avail = &ad74413r_read_avail,
  903. .update_scan_mode = &ad74413r_update_scan_mode,
  904. };
  905. #define AD74413R_DAC_CHANNEL(_type, extra_mask_separate) \
  906. { \
  907. .type = (_type), \
  908. .indexed = 1, \
  909. .output = 1, \
  910. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
  911. | (extra_mask_separate), \
  912. }
  913. #define AD74413R_ADC_CHANNEL(_type, extra_mask_separate) \
  914. { \
  915. .type = (_type), \
  916. .indexed = 1, \
  917. .output = 0, \
  918. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
  919. | BIT(IIO_CHAN_INFO_SAMP_FREQ) \
  920. | (extra_mask_separate), \
  921. .info_mask_separate_available = \
  922. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  923. .scan_type = { \
  924. .sign = 'u', \
  925. .realbits = 16, \
  926. .storagebits = 32, \
  927. .shift = 8, \
  928. .endianness = IIO_BE, \
  929. }, \
  930. }
  931. #define AD74413R_ADC_VOLTAGE_CHANNEL \
  932. AD74413R_ADC_CHANNEL(IIO_VOLTAGE, BIT(IIO_CHAN_INFO_SCALE) \
  933. | BIT(IIO_CHAN_INFO_OFFSET))
  934. #define AD74413R_ADC_CURRENT_CHANNEL \
  935. AD74413R_ADC_CHANNEL(IIO_CURRENT, BIT(IIO_CHAN_INFO_SCALE) \
  936. | BIT(IIO_CHAN_INFO_OFFSET))
  937. static struct iio_chan_spec ad74413r_voltage_output_channels[] = {
  938. AD74413R_DAC_CHANNEL(IIO_VOLTAGE, BIT(IIO_CHAN_INFO_SCALE)),
  939. AD74413R_ADC_CURRENT_CHANNEL,
  940. };
  941. static struct iio_chan_spec ad74413r_current_output_channels[] = {
  942. AD74413R_DAC_CHANNEL(IIO_CURRENT, BIT(IIO_CHAN_INFO_SCALE)),
  943. AD74413R_ADC_VOLTAGE_CHANNEL,
  944. };
  945. static struct iio_chan_spec ad74413r_voltage_input_channels[] = {
  946. AD74413R_ADC_VOLTAGE_CHANNEL,
  947. };
  948. static struct iio_chan_spec ad74413r_current_input_channels[] = {
  949. AD74413R_ADC_CURRENT_CHANNEL,
  950. };
  951. static struct iio_chan_spec ad74413r_current_input_loop_channels[] = {
  952. AD74413R_DAC_CHANNEL(IIO_CURRENT, BIT(IIO_CHAN_INFO_SCALE)),
  953. AD74413R_ADC_CURRENT_CHANNEL,
  954. };
  955. static struct iio_chan_spec ad74413r_resistance_input_channels[] = {
  956. AD74413R_ADC_CHANNEL(IIO_RESISTANCE, BIT(IIO_CHAN_INFO_PROCESSED)),
  957. };
  958. static struct iio_chan_spec ad74413r_digital_input_channels[] = {
  959. AD74413R_ADC_VOLTAGE_CHANNEL,
  960. };
  961. #define _AD74413R_CHANNELS(_channels) \
  962. { \
  963. .channels = _channels, \
  964. .num_channels = ARRAY_SIZE(_channels), \
  965. }
  966. #define AD74413R_CHANNELS(name) \
  967. _AD74413R_CHANNELS(ad74413r_ ## name ## _channels)
  968. static const struct ad74413r_channels ad74413r_channels_map[] = {
  969. [CH_FUNC_HIGH_IMPEDANCE] = AD74413R_CHANNELS(voltage_input),
  970. [CH_FUNC_VOLTAGE_OUTPUT] = AD74413R_CHANNELS(voltage_output),
  971. [CH_FUNC_CURRENT_OUTPUT] = AD74413R_CHANNELS(current_output),
  972. [CH_FUNC_VOLTAGE_INPUT] = AD74413R_CHANNELS(voltage_input),
  973. [CH_FUNC_CURRENT_INPUT_EXT_POWER] = AD74413R_CHANNELS(current_input),
  974. [CH_FUNC_CURRENT_INPUT_LOOP_POWER] = AD74413R_CHANNELS(current_input_loop),
  975. [CH_FUNC_RESISTANCE_INPUT] = AD74413R_CHANNELS(resistance_input),
  976. [CH_FUNC_DIGITAL_INPUT_LOGIC] = AD74413R_CHANNELS(digital_input),
  977. [CH_FUNC_DIGITAL_INPUT_LOOP_POWER] = AD74413R_CHANNELS(digital_input),
  978. [CH_FUNC_CURRENT_INPUT_EXT_POWER_HART] = AD74413R_CHANNELS(current_input),
  979. [CH_FUNC_CURRENT_INPUT_LOOP_POWER_HART] = AD74413R_CHANNELS(current_input),
  980. };
  981. static int ad74413r_parse_channel_config(struct iio_dev *indio_dev,
  982. struct fwnode_handle *channel_node)
  983. {
  984. struct ad74413r_state *st = iio_priv(indio_dev);
  985. struct ad74413r_channel_config *config;
  986. u32 index;
  987. int ret;
  988. ret = fwnode_property_read_u32(channel_node, "reg", &index);
  989. if (ret) {
  990. dev_err(st->dev, "Failed to read channel reg: %d\n", ret);
  991. return ret;
  992. }
  993. if (index >= AD74413R_CHANNEL_MAX) {
  994. dev_err(st->dev, "Channel index %u is too large\n", index);
  995. return -EINVAL;
  996. }
  997. config = &st->channel_configs[index];
  998. if (config->initialized) {
  999. dev_err(st->dev, "Channel %u already initialized\n", index);
  1000. return -EINVAL;
  1001. }
  1002. config->func = CH_FUNC_HIGH_IMPEDANCE;
  1003. fwnode_property_read_u32(channel_node, "adi,ch-func", &config->func);
  1004. if (config->func < CH_FUNC_MIN || config->func > CH_FUNC_MAX) {
  1005. dev_err(st->dev, "Invalid channel function %u\n", config->func);
  1006. return -EINVAL;
  1007. }
  1008. if (!st->chip_info->hart_support &&
  1009. (config->func == CH_FUNC_CURRENT_INPUT_EXT_POWER_HART ||
  1010. config->func == CH_FUNC_CURRENT_INPUT_LOOP_POWER_HART)) {
  1011. dev_err(st->dev, "Unsupported HART function %u\n", config->func);
  1012. return -EINVAL;
  1013. }
  1014. if (config->func == CH_FUNC_DIGITAL_INPUT_LOGIC ||
  1015. config->func == CH_FUNC_DIGITAL_INPUT_LOOP_POWER)
  1016. st->num_comparator_gpios++;
  1017. config->gpo_comparator = fwnode_property_read_bool(channel_node,
  1018. "adi,gpo-comparator");
  1019. fwnode_property_read_u32(channel_node, "drive-strength-microamp",
  1020. &config->drive_strength);
  1021. if (!config->gpo_comparator)
  1022. st->num_gpo_gpios++;
  1023. indio_dev->num_channels += ad74413r_channels_map[config->func].num_channels;
  1024. config->initialized = true;
  1025. return 0;
  1026. }
  1027. static int ad74413r_parse_channel_configs(struct iio_dev *indio_dev)
  1028. {
  1029. struct ad74413r_state *st = iio_priv(indio_dev);
  1030. int ret;
  1031. device_for_each_child_node_scoped(st->dev, channel_node) {
  1032. ret = ad74413r_parse_channel_config(indio_dev, channel_node);
  1033. if (ret)
  1034. return ret;
  1035. }
  1036. return 0;
  1037. }
  1038. static int ad74413r_setup_channels(struct iio_dev *indio_dev)
  1039. {
  1040. struct ad74413r_state *st = iio_priv(indio_dev);
  1041. struct ad74413r_channel_config *config;
  1042. struct iio_chan_spec *channels, *chans;
  1043. unsigned int i, num_chans, chan_i;
  1044. int ret;
  1045. channels = devm_kcalloc(st->dev, sizeof(*channels),
  1046. indio_dev->num_channels, GFP_KERNEL);
  1047. if (!channels)
  1048. return -ENOMEM;
  1049. indio_dev->channels = channels;
  1050. for (i = 0; i < AD74413R_CHANNEL_MAX; i++) {
  1051. config = &st->channel_configs[i];
  1052. chans = ad74413r_channels_map[config->func].channels;
  1053. num_chans = ad74413r_channels_map[config->func].num_channels;
  1054. memcpy(channels, chans, num_chans * sizeof(*chans));
  1055. for (chan_i = 0; chan_i < num_chans; chan_i++) {
  1056. struct iio_chan_spec *chan = &channels[chan_i];
  1057. chan->channel = i;
  1058. if (chan->output)
  1059. chan->scan_index = -1;
  1060. else
  1061. chan->scan_index = i;
  1062. }
  1063. ret = ad74413r_set_channel_function(st, i, config->func);
  1064. if (ret)
  1065. return ret;
  1066. channels += num_chans;
  1067. }
  1068. return 0;
  1069. }
  1070. static int ad74413r_setup_gpios(struct ad74413r_state *st)
  1071. {
  1072. struct ad74413r_channel_config *config;
  1073. unsigned int comp_gpio_i = 0;
  1074. unsigned int gpo_gpio_i = 0;
  1075. unsigned int i;
  1076. u8 gpo_config;
  1077. u32 strength;
  1078. int ret;
  1079. for (i = 0; i < AD74413R_CHANNEL_MAX; i++) {
  1080. config = &st->channel_configs[i];
  1081. if (config->gpo_comparator) {
  1082. gpo_config = AD74413R_GPO_CONFIG_COMPARATOR;
  1083. } else {
  1084. gpo_config = AD74413R_GPO_CONFIG_LOGIC;
  1085. st->gpo_gpio_offsets[gpo_gpio_i++] = i;
  1086. }
  1087. if (config->func == CH_FUNC_DIGITAL_INPUT_LOGIC ||
  1088. config->func == CH_FUNC_DIGITAL_INPUT_LOOP_POWER) {
  1089. st->comp_gpio_offsets[comp_gpio_i++] = i;
  1090. strength = config->drive_strength;
  1091. ret = ad74413r_set_comp_drive_strength(st, i, strength);
  1092. if (ret)
  1093. return ret;
  1094. }
  1095. ret = ad74413r_set_gpo_config(st, i, gpo_config);
  1096. if (ret)
  1097. return ret;
  1098. }
  1099. return 0;
  1100. }
  1101. static void ad74413r_regulator_disable(void *regulator)
  1102. {
  1103. regulator_disable(regulator);
  1104. }
  1105. static int ad74413r_probe(struct spi_device *spi)
  1106. {
  1107. struct ad74413r_state *st;
  1108. struct iio_dev *indio_dev;
  1109. int ret;
  1110. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  1111. if (!indio_dev)
  1112. return -ENOMEM;
  1113. st = iio_priv(indio_dev);
  1114. st->spi = spi;
  1115. st->dev = &spi->dev;
  1116. st->chip_info = spi_get_device_match_data(spi);
  1117. if (!st->chip_info)
  1118. return -EINVAL;
  1119. mutex_init(&st->lock);
  1120. init_completion(&st->adc_data_completion);
  1121. st->regmap = devm_regmap_init(st->dev, NULL, st,
  1122. &ad74413r_regmap_config);
  1123. if (IS_ERR(st->regmap))
  1124. return PTR_ERR(st->regmap);
  1125. st->reset_gpio = devm_gpiod_get_optional(st->dev, "reset", GPIOD_OUT_LOW);
  1126. if (IS_ERR(st->reset_gpio))
  1127. return PTR_ERR(st->reset_gpio);
  1128. st->refin_reg = devm_regulator_get(st->dev, "refin");
  1129. if (IS_ERR(st->refin_reg))
  1130. return dev_err_probe(st->dev, PTR_ERR(st->refin_reg),
  1131. "Failed to get refin regulator\n");
  1132. ret = regulator_enable(st->refin_reg);
  1133. if (ret)
  1134. return ret;
  1135. ret = devm_add_action_or_reset(st->dev, ad74413r_regulator_disable,
  1136. st->refin_reg);
  1137. if (ret)
  1138. return ret;
  1139. st->sense_resistor_ohms = 100000000;
  1140. device_property_read_u32(st->dev, "shunt-resistor-micro-ohms",
  1141. &st->sense_resistor_ohms);
  1142. st->sense_resistor_ohms /= 1000000;
  1143. st->trig = devm_iio_trigger_alloc(st->dev, "%s-dev%d",
  1144. st->chip_info->name, iio_device_id(indio_dev));
  1145. if (!st->trig)
  1146. return -ENOMEM;
  1147. st->trig->ops = &ad74413r_trigger_ops;
  1148. iio_trigger_set_drvdata(st->trig, st);
  1149. ret = devm_iio_trigger_register(st->dev, st->trig);
  1150. if (ret)
  1151. return ret;
  1152. indio_dev->name = st->chip_info->name;
  1153. indio_dev->modes = INDIO_DIRECT_MODE;
  1154. indio_dev->info = &ad74413r_info;
  1155. indio_dev->trig = iio_trigger_get(st->trig);
  1156. ret = ad74413r_reset(st);
  1157. if (ret)
  1158. return ret;
  1159. ret = ad74413r_parse_channel_configs(indio_dev);
  1160. if (ret)
  1161. return ret;
  1162. ret = ad74413r_setup_channels(indio_dev);
  1163. if (ret)
  1164. return ret;
  1165. ret = ad74413r_setup_gpios(st);
  1166. if (ret)
  1167. return ret;
  1168. if (st->num_gpo_gpios) {
  1169. st->gpo_gpiochip.owner = THIS_MODULE;
  1170. st->gpo_gpiochip.label = st->chip_info->name;
  1171. st->gpo_gpiochip.base = -1;
  1172. st->gpo_gpiochip.ngpio = st->num_gpo_gpios;
  1173. st->gpo_gpiochip.parent = st->dev;
  1174. st->gpo_gpiochip.can_sleep = true;
  1175. st->gpo_gpiochip.set = ad74413r_gpio_set;
  1176. st->gpo_gpiochip.set_multiple = ad74413r_gpio_set_multiple;
  1177. st->gpo_gpiochip.set_config = ad74413r_gpio_set_gpo_config;
  1178. st->gpo_gpiochip.get_direction =
  1179. ad74413r_gpio_get_gpo_direction;
  1180. ret = devm_gpiochip_add_data(st->dev, &st->gpo_gpiochip, st);
  1181. if (ret)
  1182. return ret;
  1183. }
  1184. if (st->num_comparator_gpios) {
  1185. st->comp_gpiochip.owner = THIS_MODULE;
  1186. st->comp_gpiochip.label = st->chip_info->name;
  1187. st->comp_gpiochip.base = -1;
  1188. st->comp_gpiochip.ngpio = st->num_comparator_gpios;
  1189. st->comp_gpiochip.parent = st->dev;
  1190. st->comp_gpiochip.can_sleep = true;
  1191. st->comp_gpiochip.get = ad74413r_gpio_get;
  1192. st->comp_gpiochip.get_multiple = ad74413r_gpio_get_multiple;
  1193. st->comp_gpiochip.set_config = ad74413r_gpio_set_comp_config;
  1194. st->comp_gpiochip.get_direction =
  1195. ad74413r_gpio_get_comp_direction;
  1196. ret = devm_gpiochip_add_data(st->dev, &st->comp_gpiochip, st);
  1197. if (ret)
  1198. return ret;
  1199. }
  1200. ret = ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_OFF);
  1201. if (ret)
  1202. return ret;
  1203. ret = devm_request_irq(st->dev, spi->irq, ad74413r_adc_data_interrupt,
  1204. 0, st->chip_info->name, indio_dev);
  1205. if (ret)
  1206. return dev_err_probe(st->dev, ret, "Failed to request irq\n");
  1207. ret = devm_iio_triggered_buffer_setup(st->dev, indio_dev,
  1208. &iio_pollfunc_store_time,
  1209. &ad74413r_trigger_handler,
  1210. &ad74413r_buffer_ops);
  1211. if (ret)
  1212. return ret;
  1213. return devm_iio_device_register(st->dev, indio_dev);
  1214. }
  1215. static int ad74413r_unregister_driver(struct spi_driver *spi)
  1216. {
  1217. spi_unregister_driver(spi);
  1218. return 0;
  1219. }
  1220. static int __init ad74413r_register_driver(struct spi_driver *spi)
  1221. {
  1222. crc8_populate_msb(ad74413r_crc8_table, AD74413R_CRC_POLYNOMIAL);
  1223. return spi_register_driver(spi);
  1224. }
  1225. static const struct ad74413r_chip_info ad74412r_chip_info_data = {
  1226. .hart_support = false,
  1227. .name = "ad74412r",
  1228. };
  1229. static const struct ad74413r_chip_info ad74413r_chip_info_data = {
  1230. .hart_support = true,
  1231. .name = "ad74413r",
  1232. };
  1233. static const struct of_device_id ad74413r_dt_id[] = {
  1234. {
  1235. .compatible = "adi,ad74412r",
  1236. .data = &ad74412r_chip_info_data,
  1237. },
  1238. {
  1239. .compatible = "adi,ad74413r",
  1240. .data = &ad74413r_chip_info_data,
  1241. },
  1242. {},
  1243. };
  1244. MODULE_DEVICE_TABLE(of, ad74413r_dt_id);
  1245. static const struct spi_device_id ad74413r_spi_id[] = {
  1246. { .name = "ad74412r", .driver_data = (kernel_ulong_t)&ad74412r_chip_info_data },
  1247. { .name = "ad74413r", .driver_data = (kernel_ulong_t)&ad74413r_chip_info_data },
  1248. {}
  1249. };
  1250. MODULE_DEVICE_TABLE(spi, ad74413r_spi_id);
  1251. static struct spi_driver ad74413r_driver = {
  1252. .driver = {
  1253. .name = "ad74413r",
  1254. .of_match_table = ad74413r_dt_id,
  1255. },
  1256. .probe = ad74413r_probe,
  1257. .id_table = ad74413r_spi_id,
  1258. };
  1259. module_driver(ad74413r_driver,
  1260. ad74413r_register_driver,
  1261. ad74413r_unregister_driver);
  1262. MODULE_AUTHOR("Cosmin Tanislav <cosmin.tanislav@analog.com>");
  1263. MODULE_DESCRIPTION("Analog Devices AD74413R ADDAC");
  1264. MODULE_LICENSE("GPL v2");