ad7173.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * AD717x and AD411x family SPI ADC driver
  4. *
  5. * Supported devices:
  6. * AD4111/AD4112/AD4114/AD4115/AD4116
  7. * AD7172-2/AD7172-4/AD7173-8/AD7175-2
  8. * AD7175-8/AD7176-2/AD7177-2
  9. *
  10. * Copyright (C) 2015, 2024 Analog Devices, Inc.
  11. */
  12. #include <linux/array_size.h>
  13. #include <linux/bitfield.h>
  14. #include <linux/bitmap.h>
  15. #include <linux/container_of.h>
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/delay.h>
  19. #include <linux/device.h>
  20. #include <linux/err.h>
  21. #include <linux/gpio/driver.h>
  22. #include <linux/gpio/regmap.h>
  23. #include <linux/idr.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/math64.h>
  26. #include <linux/module.h>
  27. #include <linux/mod_devicetable.h>
  28. #include <linux/property.h>
  29. #include <linux/regmap.h>
  30. #include <linux/regulator/consumer.h>
  31. #include <linux/slab.h>
  32. #include <linux/spi/spi.h>
  33. #include <linux/types.h>
  34. #include <linux/units.h>
  35. #include <linux/iio/buffer.h>
  36. #include <linux/iio/iio.h>
  37. #include <linux/iio/trigger_consumer.h>
  38. #include <linux/iio/triggered_buffer.h>
  39. #include <linux/iio/adc/ad_sigma_delta.h>
  40. #define AD7173_REG_COMMS 0x00
  41. #define AD7173_REG_ADC_MODE 0x01
  42. #define AD7173_REG_INTERFACE_MODE 0x02
  43. #define AD7173_REG_CRC 0x03
  44. #define AD7173_REG_DATA 0x04
  45. #define AD7173_REG_GPIO 0x06
  46. #define AD7173_REG_ID 0x07
  47. #define AD7173_REG_CH(x) (0x10 + (x))
  48. #define AD7173_REG_SETUP(x) (0x20 + (x))
  49. #define AD7173_REG_FILTER(x) (0x28 + (x))
  50. #define AD7173_REG_OFFSET(x) (0x30 + (x))
  51. #define AD7173_REG_GAIN(x) (0x38 + (x))
  52. #define AD7173_RESET_LENGTH BITS_TO_BYTES(64)
  53. #define AD7173_CH_ENABLE BIT(15)
  54. #define AD7173_CH_SETUP_SEL_MASK GENMASK(14, 12)
  55. #define AD7173_CH_SETUP_AINPOS_MASK GENMASK(9, 5)
  56. #define AD7173_CH_SETUP_AINNEG_MASK GENMASK(4, 0)
  57. #define AD7173_NO_AINS_PER_CHANNEL 2
  58. #define AD7173_CH_ADDRESS(pos, neg) \
  59. (FIELD_PREP(AD7173_CH_SETUP_AINPOS_MASK, pos) | \
  60. FIELD_PREP(AD7173_CH_SETUP_AINNEG_MASK, neg))
  61. #define AD7173_AIN_TEMP_POS 17
  62. #define AD7173_AIN_TEMP_NEG 18
  63. #define AD7173_AIN_POW_MON_POS 19
  64. #define AD7173_AIN_POW_MON_NEG 20
  65. #define AD7173_AIN_REF_POS 21
  66. #define AD7173_AIN_REF_NEG 22
  67. #define AD7173_IS_REF_INPUT(x) ((x) == AD7173_AIN_REF_POS || \
  68. (x) == AD7173_AIN_REF_NEG)
  69. #define AD7172_2_ID 0x00d0
  70. #define AD7175_ID 0x0cd0
  71. #define AD7176_ID 0x0c90
  72. #define AD7175_2_ID 0x0cd0
  73. #define AD7172_4_ID 0x2050
  74. #define AD7173_ID 0x30d0
  75. #define AD4111_ID AD7173_ID
  76. #define AD4112_ID AD7173_ID
  77. #define AD4114_ID AD7173_ID
  78. #define AD4116_ID 0x34d0
  79. #define AD4115_ID 0x38d0
  80. #define AD7175_8_ID 0x3cd0
  81. #define AD7177_ID 0x4fd0
  82. #define AD7173_ID_MASK GENMASK(15, 4)
  83. #define AD7173_ADC_MODE_REF_EN BIT(15)
  84. #define AD7173_ADC_MODE_SING_CYC BIT(13)
  85. #define AD7173_ADC_MODE_MODE_MASK GENMASK(6, 4)
  86. #define AD7173_ADC_MODE_CLOCKSEL_MASK GENMASK(3, 2)
  87. #define AD7173_ADC_MODE_CLOCKSEL_INT 0x0
  88. #define AD7173_ADC_MODE_CLOCKSEL_INT_OUTPUT 0x1
  89. #define AD7173_ADC_MODE_CLOCKSEL_EXT 0x2
  90. #define AD7173_ADC_MODE_CLOCKSEL_XTAL 0x3
  91. #define AD7173_GPIO_PDSW BIT(14)
  92. #define AD7173_GPIO_OP_EN2_3 BIT(13)
  93. #define AD7173_GPIO_MUX_IO BIT(12)
  94. #define AD7173_GPIO_SYNC_EN BIT(11)
  95. #define AD7173_GPIO_ERR_EN BIT(10)
  96. #define AD7173_GPIO_ERR_DAT BIT(9)
  97. #define AD7173_GPIO_GP_DATA3 BIT(7)
  98. #define AD7173_GPIO_GP_DATA2 BIT(6)
  99. #define AD7173_GPIO_IP_EN1 BIT(5)
  100. #define AD7173_GPIO_IP_EN0 BIT(4)
  101. #define AD7173_GPIO_OP_EN1 BIT(3)
  102. #define AD7173_GPIO_OP_EN0 BIT(2)
  103. #define AD7173_GPIO_GP_DATA1 BIT(1)
  104. #define AD7173_GPIO_GP_DATA0 BIT(0)
  105. #define AD7173_GPO12_DATA(x) BIT((x) + 0)
  106. #define AD7173_GPO23_DATA(x) BIT((x) + 4)
  107. #define AD4111_GPO01_DATA(x) BIT((x) + 6)
  108. #define AD7173_GPO_DATA(x) ((x) < 2 ? AD7173_GPO12_DATA(x) : AD7173_GPO23_DATA(x))
  109. #define AD7173_INTERFACE_DATA_STAT BIT(6)
  110. #define AD7173_INTERFACE_DATA_STAT_EN(x) \
  111. FIELD_PREP(AD7173_INTERFACE_DATA_STAT, x)
  112. #define AD7173_SETUP_BIPOLAR BIT(12)
  113. #define AD7173_SETUP_AREF_BUF_MASK GENMASK(11, 10)
  114. #define AD7173_SETUP_AIN_BUF_MASK GENMASK(9, 8)
  115. #define AD7173_SETUP_REF_SEL_MASK GENMASK(5, 4)
  116. #define AD7173_SETUP_REF_SEL_AVDD1_AVSS 0x3
  117. #define AD7173_SETUP_REF_SEL_INT_REF 0x2
  118. #define AD7173_SETUP_REF_SEL_EXT_REF2 0x1
  119. #define AD7173_SETUP_REF_SEL_EXT_REF 0x0
  120. #define AD7173_VOLTAGE_INT_REF_uV 2500000
  121. #define AD7173_TEMP_SENSIIVITY_uV_per_C 477
  122. #define AD7177_ODR_START_VALUE 0x07
  123. #define AD4111_SHUNT_RESISTOR_OHM 50
  124. #define AD4111_DIVIDER_RATIO 10
  125. #define AD4111_CURRENT_CHAN_CUTOFF 16
  126. #define AD4111_VINCOM_INPUT 0x10
  127. /* pin < num_voltage_in is a normal voltage input */
  128. /* pin >= num_voltage_in_div is a voltage input without a divider */
  129. #define AD4111_IS_VINCOM_MISMATCH(pin1, pin2) ((pin1) == AD4111_VINCOM_INPUT && \
  130. (pin2) < st->info->num_voltage_in && \
  131. (pin2) >= st->info->num_voltage_in_div)
  132. #define AD7173_FILTER_ODR0_MASK GENMASK(5, 0)
  133. #define AD7173_MAX_CONFIGS 8
  134. struct ad7173_device_info {
  135. const unsigned int *sinc5_data_rates;
  136. unsigned int num_sinc5_data_rates;
  137. unsigned int odr_start_value;
  138. /*
  139. * AD4116 has both inputs with a voltage divider and without.
  140. * These inputs cannot be mixed in the channel configuration.
  141. * Does not include the VINCOM input.
  142. */
  143. unsigned int num_voltage_in_div;
  144. unsigned int num_channels;
  145. unsigned int num_configs;
  146. unsigned int num_voltage_in;
  147. unsigned int clock;
  148. unsigned int id;
  149. char *name;
  150. bool has_current_inputs;
  151. bool has_vincom_input;
  152. bool has_temp;
  153. /* ((AVDD1 − AVSS)/5) */
  154. bool has_pow_supply_monitoring;
  155. bool has_input_buf;
  156. bool has_int_ref;
  157. bool has_ref2;
  158. bool higher_gpio_bits;
  159. u8 num_gpios;
  160. };
  161. struct ad7173_channel_config {
  162. u8 cfg_slot;
  163. bool live;
  164. /* Following fields are used to compare equality. */
  165. struct_group(config_props,
  166. bool bipolar;
  167. bool input_buf;
  168. u8 odr;
  169. u8 ref_sel;
  170. );
  171. };
  172. struct ad7173_channel {
  173. unsigned int chan_reg;
  174. unsigned int ain;
  175. struct ad7173_channel_config cfg;
  176. };
  177. struct ad7173_state {
  178. struct ad_sigma_delta sd;
  179. struct ad_sigma_delta_info sigma_delta_info;
  180. const struct ad7173_device_info *info;
  181. struct ad7173_channel *channels;
  182. struct regulator_bulk_data regulators[3];
  183. unsigned int adc_mode;
  184. unsigned int interface_mode;
  185. unsigned int num_channels;
  186. struct ida cfg_slots_status;
  187. unsigned long long config_usage_counter;
  188. unsigned long long *config_cnts;
  189. struct clk *ext_clk;
  190. struct clk_hw int_clk_hw;
  191. #if IS_ENABLED(CONFIG_GPIOLIB)
  192. struct regmap *reg_gpiocon_regmap;
  193. struct gpio_regmap *gpio_regmap;
  194. #endif
  195. };
  196. static unsigned int ad4115_sinc5_data_rates[] = {
  197. 24845000, 24845000, 20725000, 20725000, /* 0-3 */
  198. 15564000, 13841000, 10390000, 10390000, /* 4-7 */
  199. 4994000, 2499000, 1000000, 500000, /* 8-11 */
  200. 395500, 200000, 100000, 59890, /* 12-15 */
  201. 49920, 20000, 16660, 10000, /* 16-19 */
  202. 5000, 2500, 2500, /* 20-22 */
  203. };
  204. static unsigned int ad4116_sinc5_data_rates[] = {
  205. 12422360, 12422360, 12422360, 12422360, /* 0-3 */
  206. 10362690, 10362690, 7782100, 6290530, /* 4-7 */
  207. 5194800, 2496900, 1007600, 499900, /* 8-11 */
  208. 390600, 200300, 100000, 59750, /* 12-15 */
  209. 49840, 20000, 16650, 10000, /* 16-19 */
  210. 5000, 2500, 1250, /* 20-22 */
  211. };
  212. static const unsigned int ad7173_sinc5_data_rates[] = {
  213. 6211000, 6211000, 6211000, 6211000, 6211000, 6211000, 5181000, 4444000, /* 0-7 */
  214. 3115000, 2597000, 1007000, 503800, 381000, 200300, 100500, 59520, /* 8-15 */
  215. 49680, 20010, 16333, 10000, 5000, 2500, 1250, /* 16-22 */
  216. };
  217. static const unsigned int ad7175_sinc5_data_rates[] = {
  218. 50000000, 41667000, 31250000, 27778000, /* 0-3 */
  219. 20833000, 17857000, 12500000, 10000000, /* 4-7 */
  220. 5000000, 2500000, 1000000, 500000, /* 8-11 */
  221. 397500, 200000, 100000, 59920, /* 12-15 */
  222. 49960, 20000, 16666, 10000, /* 16-19 */
  223. 5000, /* 20 */
  224. };
  225. static unsigned int ad4111_current_channel_config[] = {
  226. /* Ain sel: pos neg */
  227. 0x1E8, /* 15:IIN0+ 8:IIN0− */
  228. 0x1C9, /* 14:IIN1+ 9:IIN1− */
  229. 0x1AA, /* 13:IIN2+ 10:IIN2− */
  230. 0x18B, /* 12:IIN3+ 11:IIN3− */
  231. };
  232. static const struct ad7173_device_info ad4111_device_info = {
  233. .name = "ad4111",
  234. .id = AD4111_ID,
  235. .num_voltage_in_div = 8,
  236. .num_channels = 16,
  237. .num_configs = 8,
  238. .num_voltage_in = 8,
  239. .num_gpios = 2,
  240. .higher_gpio_bits = true,
  241. .has_temp = true,
  242. .has_vincom_input = true,
  243. .has_input_buf = true,
  244. .has_current_inputs = true,
  245. .has_int_ref = true,
  246. .clock = 2 * HZ_PER_MHZ,
  247. .sinc5_data_rates = ad7173_sinc5_data_rates,
  248. .num_sinc5_data_rates = ARRAY_SIZE(ad7173_sinc5_data_rates),
  249. };
  250. static const struct ad7173_device_info ad4112_device_info = {
  251. .name = "ad4112",
  252. .id = AD4112_ID,
  253. .num_voltage_in_div = 8,
  254. .num_channels = 16,
  255. .num_configs = 8,
  256. .num_voltage_in = 8,
  257. .num_gpios = 2,
  258. .higher_gpio_bits = true,
  259. .has_vincom_input = true,
  260. .has_temp = true,
  261. .has_input_buf = true,
  262. .has_current_inputs = true,
  263. .has_int_ref = true,
  264. .clock = 2 * HZ_PER_MHZ,
  265. .sinc5_data_rates = ad7173_sinc5_data_rates,
  266. .num_sinc5_data_rates = ARRAY_SIZE(ad7173_sinc5_data_rates),
  267. };
  268. static const struct ad7173_device_info ad4114_device_info = {
  269. .name = "ad4114",
  270. .id = AD4114_ID,
  271. .num_voltage_in_div = 16,
  272. .num_channels = 16,
  273. .num_configs = 8,
  274. .num_voltage_in = 16,
  275. .num_gpios = 4,
  276. .has_vincom_input = true,
  277. .has_temp = true,
  278. .has_input_buf = true,
  279. .has_int_ref = true,
  280. .clock = 2 * HZ_PER_MHZ,
  281. .sinc5_data_rates = ad7173_sinc5_data_rates,
  282. .num_sinc5_data_rates = ARRAY_SIZE(ad7173_sinc5_data_rates),
  283. };
  284. static const struct ad7173_device_info ad4115_device_info = {
  285. .name = "ad4115",
  286. .id = AD4115_ID,
  287. .num_voltage_in_div = 16,
  288. .num_channels = 16,
  289. .num_configs = 8,
  290. .num_voltage_in = 16,
  291. .num_gpios = 4,
  292. .has_vincom_input = true,
  293. .has_temp = true,
  294. .has_input_buf = true,
  295. .has_int_ref = true,
  296. .clock = 8 * HZ_PER_MHZ,
  297. .sinc5_data_rates = ad4115_sinc5_data_rates,
  298. .num_sinc5_data_rates = ARRAY_SIZE(ad4115_sinc5_data_rates),
  299. };
  300. static const struct ad7173_device_info ad4116_device_info = {
  301. .name = "ad4116",
  302. .id = AD4116_ID,
  303. .num_voltage_in_div = 11,
  304. .num_channels = 16,
  305. .num_configs = 8,
  306. .num_voltage_in = 16,
  307. .num_gpios = 4,
  308. .has_vincom_input = true,
  309. .has_temp = true,
  310. .has_input_buf = true,
  311. .has_int_ref = true,
  312. .clock = 4 * HZ_PER_MHZ,
  313. .sinc5_data_rates = ad4116_sinc5_data_rates,
  314. .num_sinc5_data_rates = ARRAY_SIZE(ad4116_sinc5_data_rates),
  315. };
  316. static const struct ad7173_device_info ad7172_2_device_info = {
  317. .name = "ad7172-2",
  318. .id = AD7172_2_ID,
  319. .num_voltage_in = 5,
  320. .num_channels = 4,
  321. .num_configs = 4,
  322. .num_gpios = 2,
  323. .has_temp = true,
  324. .has_input_buf = true,
  325. .has_int_ref = true,
  326. .has_pow_supply_monitoring = true,
  327. .clock = 2 * HZ_PER_MHZ,
  328. .sinc5_data_rates = ad7173_sinc5_data_rates,
  329. .num_sinc5_data_rates = ARRAY_SIZE(ad7173_sinc5_data_rates),
  330. };
  331. static const struct ad7173_device_info ad7172_4_device_info = {
  332. .name = "ad7172-4",
  333. .id = AD7172_4_ID,
  334. .num_voltage_in = 9,
  335. .num_channels = 8,
  336. .num_configs = 8,
  337. .num_gpios = 4,
  338. .has_input_buf = true,
  339. .has_ref2 = true,
  340. .has_pow_supply_monitoring = true,
  341. .clock = 2 * HZ_PER_MHZ,
  342. .sinc5_data_rates = ad7173_sinc5_data_rates,
  343. .num_sinc5_data_rates = ARRAY_SIZE(ad7173_sinc5_data_rates),
  344. };
  345. static const struct ad7173_device_info ad7173_8_device_info = {
  346. .name = "ad7173-8",
  347. .id = AD7173_ID,
  348. .num_voltage_in = 17,
  349. .num_channels = 16,
  350. .num_configs = 8,
  351. .num_gpios = 4,
  352. .has_temp = true,
  353. .has_input_buf = true,
  354. .has_int_ref = true,
  355. .has_ref2 = true,
  356. .clock = 2 * HZ_PER_MHZ,
  357. .sinc5_data_rates = ad7173_sinc5_data_rates,
  358. .num_sinc5_data_rates = ARRAY_SIZE(ad7173_sinc5_data_rates),
  359. };
  360. static const struct ad7173_device_info ad7175_2_device_info = {
  361. .name = "ad7175-2",
  362. .id = AD7175_2_ID,
  363. .num_voltage_in = 5,
  364. .num_channels = 4,
  365. .num_configs = 4,
  366. .num_gpios = 2,
  367. .has_temp = true,
  368. .has_input_buf = true,
  369. .has_int_ref = true,
  370. .has_pow_supply_monitoring = true,
  371. .clock = 16 * HZ_PER_MHZ,
  372. .sinc5_data_rates = ad7175_sinc5_data_rates,
  373. .num_sinc5_data_rates = ARRAY_SIZE(ad7175_sinc5_data_rates),
  374. };
  375. static const struct ad7173_device_info ad7175_8_device_info = {
  376. .name = "ad7175-8",
  377. .id = AD7175_8_ID,
  378. .num_voltage_in = 17,
  379. .num_channels = 16,
  380. .num_configs = 8,
  381. .num_gpios = 4,
  382. .has_temp = true,
  383. .has_input_buf = true,
  384. .has_int_ref = true,
  385. .has_ref2 = true,
  386. .has_pow_supply_monitoring = true,
  387. .clock = 16 * HZ_PER_MHZ,
  388. .sinc5_data_rates = ad7175_sinc5_data_rates,
  389. .num_sinc5_data_rates = ARRAY_SIZE(ad7175_sinc5_data_rates),
  390. };
  391. static const struct ad7173_device_info ad7176_2_device_info = {
  392. .name = "ad7176-2",
  393. .id = AD7176_ID,
  394. .num_voltage_in = 5,
  395. .num_channels = 4,
  396. .num_configs = 4,
  397. .num_gpios = 2,
  398. .has_int_ref = true,
  399. .clock = 16 * HZ_PER_MHZ,
  400. .sinc5_data_rates = ad7175_sinc5_data_rates,
  401. .num_sinc5_data_rates = ARRAY_SIZE(ad7175_sinc5_data_rates),
  402. };
  403. static const struct ad7173_device_info ad7177_2_device_info = {
  404. .name = "ad7177-2",
  405. .id = AD7177_ID,
  406. .num_voltage_in = 5,
  407. .num_channels = 4,
  408. .num_configs = 4,
  409. .num_gpios = 2,
  410. .has_temp = true,
  411. .has_input_buf = true,
  412. .has_int_ref = true,
  413. .has_pow_supply_monitoring = true,
  414. .clock = 16 * HZ_PER_MHZ,
  415. .odr_start_value = AD7177_ODR_START_VALUE,
  416. .sinc5_data_rates = ad7175_sinc5_data_rates,
  417. .num_sinc5_data_rates = ARRAY_SIZE(ad7175_sinc5_data_rates),
  418. };
  419. static const char *const ad7173_ref_sel_str[] = {
  420. [AD7173_SETUP_REF_SEL_EXT_REF] = "vref",
  421. [AD7173_SETUP_REF_SEL_EXT_REF2] = "vref2",
  422. [AD7173_SETUP_REF_SEL_INT_REF] = "refout-avss",
  423. [AD7173_SETUP_REF_SEL_AVDD1_AVSS] = "avdd",
  424. };
  425. static const char *const ad7173_clk_sel[] = {
  426. "ext-clk", "xtal"
  427. };
  428. #if IS_ENABLED(CONFIG_GPIOLIB)
  429. static const struct regmap_range ad7173_range_gpio[] = {
  430. regmap_reg_range(AD7173_REG_GPIO, AD7173_REG_GPIO),
  431. };
  432. static const struct regmap_access_table ad7173_access_table = {
  433. .yes_ranges = ad7173_range_gpio,
  434. .n_yes_ranges = ARRAY_SIZE(ad7173_range_gpio),
  435. };
  436. static const struct regmap_config ad7173_regmap_config = {
  437. .reg_bits = 8,
  438. .val_bits = 16,
  439. .rd_table = &ad7173_access_table,
  440. .wr_table = &ad7173_access_table,
  441. .read_flag_mask = BIT(6),
  442. };
  443. static int ad7173_mask_xlate(struct gpio_regmap *gpio, unsigned int base,
  444. unsigned int offset, unsigned int *reg,
  445. unsigned int *mask)
  446. {
  447. *mask = AD7173_GPO_DATA(offset);
  448. *reg = base;
  449. return 0;
  450. }
  451. static int ad4111_mask_xlate(struct gpio_regmap *gpio, unsigned int base,
  452. unsigned int offset, unsigned int *reg,
  453. unsigned int *mask)
  454. {
  455. *mask = AD4111_GPO01_DATA(offset);
  456. *reg = base;
  457. return 0;
  458. }
  459. static void ad7173_gpio_disable(void *data)
  460. {
  461. struct ad7173_state *st = data;
  462. unsigned int mask;
  463. mask = AD7173_GPIO_OP_EN0 | AD7173_GPIO_OP_EN1 | AD7173_GPIO_OP_EN2_3;
  464. regmap_update_bits(st->reg_gpiocon_regmap, AD7173_REG_GPIO, mask, ~mask);
  465. }
  466. static int ad7173_gpio_init(struct ad7173_state *st)
  467. {
  468. struct gpio_regmap_config gpio_regmap = {};
  469. struct device *dev = &st->sd.spi->dev;
  470. unsigned int mask;
  471. int ret;
  472. st->reg_gpiocon_regmap = devm_regmap_init_spi(st->sd.spi, &ad7173_regmap_config);
  473. ret = PTR_ERR_OR_ZERO(st->reg_gpiocon_regmap);
  474. if (ret)
  475. return dev_err_probe(dev, ret, "Unable to init regmap\n");
  476. mask = AD7173_GPIO_OP_EN0 | AD7173_GPIO_OP_EN1 | AD7173_GPIO_OP_EN2_3;
  477. regmap_update_bits(st->reg_gpiocon_regmap, AD7173_REG_GPIO, mask, mask);
  478. ret = devm_add_action_or_reset(dev, ad7173_gpio_disable, st);
  479. if (ret)
  480. return ret;
  481. gpio_regmap.parent = dev;
  482. gpio_regmap.regmap = st->reg_gpiocon_regmap;
  483. gpio_regmap.ngpio = st->info->num_gpios;
  484. gpio_regmap.reg_set_base = AD7173_REG_GPIO;
  485. if (st->info->higher_gpio_bits)
  486. gpio_regmap.reg_mask_xlate = ad4111_mask_xlate;
  487. else
  488. gpio_regmap.reg_mask_xlate = ad7173_mask_xlate;
  489. st->gpio_regmap = devm_gpio_regmap_register(dev, &gpio_regmap);
  490. ret = PTR_ERR_OR_ZERO(st->gpio_regmap);
  491. if (ret)
  492. return dev_err_probe(dev, ret, "Unable to init gpio-regmap\n");
  493. return 0;
  494. }
  495. #else
  496. static int ad7173_gpio_init(struct ad7173_state *st)
  497. {
  498. return 0;
  499. }
  500. #endif /* CONFIG_GPIOLIB */
  501. static struct ad7173_state *ad_sigma_delta_to_ad7173(struct ad_sigma_delta *sd)
  502. {
  503. return container_of(sd, struct ad7173_state, sd);
  504. }
  505. static struct ad7173_state *clk_hw_to_ad7173(struct clk_hw *hw)
  506. {
  507. return container_of(hw, struct ad7173_state, int_clk_hw);
  508. }
  509. static void ad7173_ida_destroy(void *data)
  510. {
  511. struct ad7173_state *st = data;
  512. ida_destroy(&st->cfg_slots_status);
  513. }
  514. static void ad7173_reset_usage_cnts(struct ad7173_state *st)
  515. {
  516. memset64(st->config_cnts, 0, st->info->num_configs);
  517. st->config_usage_counter = 0;
  518. }
  519. static struct ad7173_channel_config *
  520. ad7173_find_live_config(struct ad7173_state *st, struct ad7173_channel_config *cfg)
  521. {
  522. struct ad7173_channel_config *cfg_aux;
  523. ptrdiff_t cmp_size;
  524. int i;
  525. cmp_size = sizeof_field(struct ad7173_channel_config, config_props);
  526. for (i = 0; i < st->num_channels; i++) {
  527. cfg_aux = &st->channels[i].cfg;
  528. if (cfg_aux->live &&
  529. !memcmp(&cfg->config_props, &cfg_aux->config_props, cmp_size))
  530. return cfg_aux;
  531. }
  532. return NULL;
  533. }
  534. /* Could be replaced with a generic LRU implementation */
  535. static int ad7173_free_config_slot_lru(struct ad7173_state *st)
  536. {
  537. int i, lru_position = 0;
  538. for (i = 1; i < st->info->num_configs; i++)
  539. if (st->config_cnts[i] < st->config_cnts[lru_position])
  540. lru_position = i;
  541. for (i = 0; i < st->num_channels; i++)
  542. if (st->channels[i].cfg.cfg_slot == lru_position)
  543. st->channels[i].cfg.live = false;
  544. ida_free(&st->cfg_slots_status, lru_position);
  545. return ida_alloc(&st->cfg_slots_status, GFP_KERNEL);
  546. }
  547. /* Could be replaced with a generic LRU implementation */
  548. static int ad7173_load_config(struct ad7173_state *st,
  549. struct ad7173_channel_config *cfg)
  550. {
  551. unsigned int config;
  552. int free_cfg_slot, ret;
  553. free_cfg_slot = ida_alloc_range(&st->cfg_slots_status, 0,
  554. st->info->num_configs - 1, GFP_KERNEL);
  555. if (free_cfg_slot < 0)
  556. free_cfg_slot = ad7173_free_config_slot_lru(st);
  557. cfg->cfg_slot = free_cfg_slot;
  558. config = FIELD_PREP(AD7173_SETUP_REF_SEL_MASK, cfg->ref_sel);
  559. if (cfg->bipolar)
  560. config |= AD7173_SETUP_BIPOLAR;
  561. if (cfg->input_buf)
  562. config |= AD7173_SETUP_AIN_BUF_MASK;
  563. ret = ad_sd_write_reg(&st->sd, AD7173_REG_SETUP(free_cfg_slot), 2, config);
  564. if (ret)
  565. return ret;
  566. return ad_sd_write_reg(&st->sd, AD7173_REG_FILTER(free_cfg_slot), 2,
  567. AD7173_FILTER_ODR0_MASK & cfg->odr);
  568. }
  569. static int ad7173_config_channel(struct ad7173_state *st, int addr)
  570. {
  571. struct ad7173_channel_config *cfg = &st->channels[addr].cfg;
  572. struct ad7173_channel_config *live_cfg;
  573. int ret;
  574. if (!cfg->live) {
  575. live_cfg = ad7173_find_live_config(st, cfg);
  576. if (live_cfg) {
  577. cfg->cfg_slot = live_cfg->cfg_slot;
  578. } else {
  579. ret = ad7173_load_config(st, cfg);
  580. if (ret)
  581. return ret;
  582. cfg->live = true;
  583. }
  584. }
  585. if (st->config_usage_counter == U64_MAX)
  586. ad7173_reset_usage_cnts(st);
  587. st->config_usage_counter++;
  588. st->config_cnts[cfg->cfg_slot] = st->config_usage_counter;
  589. return 0;
  590. }
  591. static int ad7173_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
  592. {
  593. struct ad7173_state *st = ad_sigma_delta_to_ad7173(sd);
  594. unsigned int val;
  595. int ret;
  596. ret = ad7173_config_channel(st, channel);
  597. if (ret)
  598. return ret;
  599. val = AD7173_CH_ENABLE |
  600. FIELD_PREP(AD7173_CH_SETUP_SEL_MASK, st->channels[channel].cfg.cfg_slot) |
  601. st->channels[channel].ain;
  602. return ad_sd_write_reg(&st->sd, AD7173_REG_CH(channel), 2, val);
  603. }
  604. static int ad7173_set_mode(struct ad_sigma_delta *sd,
  605. enum ad_sigma_delta_mode mode)
  606. {
  607. struct ad7173_state *st = ad_sigma_delta_to_ad7173(sd);
  608. st->adc_mode &= ~AD7173_ADC_MODE_MODE_MASK;
  609. st->adc_mode |= FIELD_PREP(AD7173_ADC_MODE_MODE_MASK, mode);
  610. return ad_sd_write_reg(&st->sd, AD7173_REG_ADC_MODE, 2, st->adc_mode);
  611. }
  612. static int ad7173_append_status(struct ad_sigma_delta *sd, bool append)
  613. {
  614. struct ad7173_state *st = ad_sigma_delta_to_ad7173(sd);
  615. unsigned int interface_mode = st->interface_mode;
  616. int ret;
  617. interface_mode &= ~AD7173_INTERFACE_DATA_STAT;
  618. interface_mode |= AD7173_INTERFACE_DATA_STAT_EN(append);
  619. ret = ad_sd_write_reg(&st->sd, AD7173_REG_INTERFACE_MODE, 2, interface_mode);
  620. if (ret)
  621. return ret;
  622. st->interface_mode = interface_mode;
  623. return 0;
  624. }
  625. static int ad7173_disable_all(struct ad_sigma_delta *sd)
  626. {
  627. struct ad7173_state *st = ad_sigma_delta_to_ad7173(sd);
  628. int ret;
  629. int i;
  630. for (i = 0; i < st->num_channels; i++) {
  631. ret = ad_sd_write_reg(sd, AD7173_REG_CH(i), 2, 0);
  632. if (ret < 0)
  633. return ret;
  634. }
  635. return 0;
  636. }
  637. static int ad7173_disable_one(struct ad_sigma_delta *sd, unsigned int chan)
  638. {
  639. return ad_sd_write_reg(sd, AD7173_REG_CH(chan), 2, 0);
  640. }
  641. static const struct ad_sigma_delta_info ad7173_sigma_delta_info = {
  642. .set_channel = ad7173_set_channel,
  643. .append_status = ad7173_append_status,
  644. .disable_all = ad7173_disable_all,
  645. .disable_one = ad7173_disable_one,
  646. .set_mode = ad7173_set_mode,
  647. .has_registers = true,
  648. .addr_shift = 0,
  649. .read_mask = BIT(6),
  650. .status_ch_mask = GENMASK(3, 0),
  651. .data_reg = AD7173_REG_DATA,
  652. };
  653. static int ad7173_setup(struct iio_dev *indio_dev)
  654. {
  655. struct ad7173_state *st = iio_priv(indio_dev);
  656. struct device *dev = &st->sd.spi->dev;
  657. u8 buf[AD7173_RESET_LENGTH];
  658. unsigned int id;
  659. int ret;
  660. /* reset the serial interface */
  661. memset(buf, 0xff, AD7173_RESET_LENGTH);
  662. ret = spi_write_then_read(st->sd.spi, buf, sizeof(buf), NULL, 0);
  663. if (ret < 0)
  664. return ret;
  665. /* datasheet recommends a delay of at least 500us after reset */
  666. fsleep(500);
  667. ret = ad_sd_read_reg(&st->sd, AD7173_REG_ID, 2, &id);
  668. if (ret)
  669. return ret;
  670. id &= AD7173_ID_MASK;
  671. if (id != st->info->id)
  672. dev_warn(dev, "Unexpected device id: 0x%04X, expected: 0x%04X\n",
  673. id, st->info->id);
  674. st->adc_mode |= AD7173_ADC_MODE_SING_CYC;
  675. st->interface_mode = 0x0;
  676. st->config_usage_counter = 0;
  677. st->config_cnts = devm_kcalloc(dev, st->info->num_configs,
  678. sizeof(*st->config_cnts), GFP_KERNEL);
  679. if (!st->config_cnts)
  680. return -ENOMEM;
  681. /* All channels are enabled by default after a reset */
  682. return ad7173_disable_all(&st->sd);
  683. }
  684. static unsigned int ad7173_get_ref_voltage_milli(struct ad7173_state *st,
  685. u8 reference_select)
  686. {
  687. int vref;
  688. switch (reference_select) {
  689. case AD7173_SETUP_REF_SEL_EXT_REF:
  690. vref = regulator_get_voltage(st->regulators[0].consumer);
  691. break;
  692. case AD7173_SETUP_REF_SEL_EXT_REF2:
  693. vref = regulator_get_voltage(st->regulators[1].consumer);
  694. break;
  695. case AD7173_SETUP_REF_SEL_INT_REF:
  696. vref = AD7173_VOLTAGE_INT_REF_uV;
  697. break;
  698. case AD7173_SETUP_REF_SEL_AVDD1_AVSS:
  699. vref = regulator_get_voltage(st->regulators[2].consumer);
  700. break;
  701. default:
  702. return -EINVAL;
  703. }
  704. if (vref < 0)
  705. return vref;
  706. return vref / (MICRO / MILLI);
  707. }
  708. static int ad7173_read_raw(struct iio_dev *indio_dev,
  709. struct iio_chan_spec const *chan,
  710. int *val, int *val2, long info)
  711. {
  712. struct ad7173_state *st = iio_priv(indio_dev);
  713. struct ad7173_channel *ch = &st->channels[chan->address];
  714. unsigned int reg;
  715. u64 temp;
  716. int ret;
  717. switch (info) {
  718. case IIO_CHAN_INFO_RAW:
  719. ret = ad_sigma_delta_single_conversion(indio_dev, chan, val);
  720. if (ret < 0)
  721. return ret;
  722. return IIO_VAL_INT;
  723. case IIO_CHAN_INFO_SCALE:
  724. switch (chan->type) {
  725. case IIO_TEMP:
  726. temp = AD7173_VOLTAGE_INT_REF_uV * MILLI;
  727. temp /= AD7173_TEMP_SENSIIVITY_uV_per_C;
  728. *val = temp;
  729. *val2 = chan->scan_type.realbits;
  730. return IIO_VAL_FRACTIONAL_LOG2;
  731. case IIO_VOLTAGE:
  732. *val = ad7173_get_ref_voltage_milli(st, ch->cfg.ref_sel);
  733. *val2 = chan->scan_type.realbits - !!(ch->cfg.bipolar);
  734. if (chan->channel < st->info->num_voltage_in_div)
  735. *val *= AD4111_DIVIDER_RATIO;
  736. return IIO_VAL_FRACTIONAL_LOG2;
  737. case IIO_CURRENT:
  738. *val = ad7173_get_ref_voltage_milli(st, ch->cfg.ref_sel);
  739. *val /= AD4111_SHUNT_RESISTOR_OHM;
  740. *val2 = chan->scan_type.realbits - ch->cfg.bipolar;
  741. return IIO_VAL_FRACTIONAL_LOG2;
  742. default:
  743. return -EINVAL;
  744. }
  745. case IIO_CHAN_INFO_OFFSET:
  746. switch (chan->type) {
  747. case IIO_TEMP:
  748. /* 0 Kelvin -> raw sample */
  749. temp = -ABSOLUTE_ZERO_MILLICELSIUS;
  750. temp *= AD7173_TEMP_SENSIIVITY_uV_per_C;
  751. temp <<= chan->scan_type.realbits;
  752. temp = DIV_U64_ROUND_CLOSEST(temp,
  753. AD7173_VOLTAGE_INT_REF_uV *
  754. MILLI);
  755. *val = -temp;
  756. return IIO_VAL_INT;
  757. case IIO_VOLTAGE:
  758. case IIO_CURRENT:
  759. *val = -BIT(chan->scan_type.realbits - 1);
  760. return IIO_VAL_INT;
  761. default:
  762. return -EINVAL;
  763. }
  764. case IIO_CHAN_INFO_SAMP_FREQ:
  765. reg = st->channels[chan->address].cfg.odr;
  766. *val = st->info->sinc5_data_rates[reg] / MILLI;
  767. *val2 = (st->info->sinc5_data_rates[reg] % MILLI) * (MICRO / MILLI);
  768. return IIO_VAL_INT_PLUS_MICRO;
  769. default:
  770. return -EINVAL;
  771. }
  772. }
  773. static int ad7173_write_raw(struct iio_dev *indio_dev,
  774. struct iio_chan_spec const *chan,
  775. int val, int val2, long info)
  776. {
  777. struct ad7173_state *st = iio_priv(indio_dev);
  778. struct ad7173_channel_config *cfg;
  779. unsigned int freq, i;
  780. int ret;
  781. ret = iio_device_claim_direct_mode(indio_dev);
  782. if (ret)
  783. return ret;
  784. switch (info) {
  785. /*
  786. * This attribute sets the sampling frequency for each channel individually.
  787. * There are no issues for raw or buffered reads of an individual channel.
  788. *
  789. * When multiple channels are enabled in buffered mode, the effective
  790. * sampling rate of a channel is lowered in correlation to the number
  791. * of channels enabled and the sampling rate of the other channels.
  792. *
  793. * Example: 3 channels enabled with rates CH1:6211sps CH2,CH3:10sps
  794. * While the reading of CH1 takes only 0.16ms, the reading of CH2 and CH3
  795. * will take 100ms each.
  796. *
  797. * This will cause the reading of CH1 to be actually done once every
  798. * 200.16ms, an effective rate of 4.99sps.
  799. */
  800. case IIO_CHAN_INFO_SAMP_FREQ:
  801. freq = val * MILLI + val2 / MILLI;
  802. for (i = st->info->odr_start_value; i < st->info->num_sinc5_data_rates - 1; i++)
  803. if (freq >= st->info->sinc5_data_rates[i])
  804. break;
  805. cfg = &st->channels[chan->address].cfg;
  806. cfg->odr = i;
  807. cfg->live = false;
  808. break;
  809. default:
  810. ret = -EINVAL;
  811. break;
  812. }
  813. iio_device_release_direct_mode(indio_dev);
  814. return ret;
  815. }
  816. static int ad7173_update_scan_mode(struct iio_dev *indio_dev,
  817. const unsigned long *scan_mask)
  818. {
  819. struct ad7173_state *st = iio_priv(indio_dev);
  820. int i, ret;
  821. for (i = 0; i < indio_dev->num_channels; i++) {
  822. if (test_bit(i, scan_mask))
  823. ret = ad7173_set_channel(&st->sd, i);
  824. else
  825. ret = ad_sd_write_reg(&st->sd, AD7173_REG_CH(i), 2, 0);
  826. if (ret < 0)
  827. return ret;
  828. }
  829. return 0;
  830. }
  831. static int ad7173_debug_reg_access(struct iio_dev *indio_dev, unsigned int reg,
  832. unsigned int writeval, unsigned int *readval)
  833. {
  834. struct ad7173_state *st = iio_priv(indio_dev);
  835. u8 reg_size;
  836. if (reg == AD7173_REG_COMMS)
  837. reg_size = 1;
  838. else if (reg == AD7173_REG_CRC || reg == AD7173_REG_DATA ||
  839. reg >= AD7173_REG_OFFSET(0))
  840. reg_size = 3;
  841. else
  842. reg_size = 2;
  843. if (readval)
  844. return ad_sd_read_reg(&st->sd, reg, reg_size, readval);
  845. return ad_sd_write_reg(&st->sd, reg, reg_size, writeval);
  846. }
  847. static const struct iio_info ad7173_info = {
  848. .read_raw = &ad7173_read_raw,
  849. .write_raw = &ad7173_write_raw,
  850. .debugfs_reg_access = &ad7173_debug_reg_access,
  851. .validate_trigger = ad_sd_validate_trigger,
  852. .update_scan_mode = ad7173_update_scan_mode,
  853. };
  854. static const struct iio_chan_spec ad7173_channel_template = {
  855. .type = IIO_VOLTAGE,
  856. .indexed = 1,
  857. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  858. BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
  859. .scan_type = {
  860. .sign = 'u',
  861. .realbits = 24,
  862. .storagebits = 32,
  863. .endianness = IIO_BE,
  864. },
  865. };
  866. static const struct iio_chan_spec ad7173_temp_iio_channel_template = {
  867. .type = IIO_TEMP,
  868. .channel = AD7173_AIN_TEMP_POS,
  869. .channel2 = AD7173_AIN_TEMP_NEG,
  870. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  871. BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET) |
  872. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  873. .scan_type = {
  874. .sign = 'u',
  875. .realbits = 24,
  876. .storagebits = 32,
  877. .endianness = IIO_BE,
  878. },
  879. };
  880. static void ad7173_disable_regulators(void *data)
  881. {
  882. struct ad7173_state *st = data;
  883. regulator_bulk_disable(ARRAY_SIZE(st->regulators), st->regulators);
  884. }
  885. static void ad7173_clk_disable_unprepare(void *clk)
  886. {
  887. clk_disable_unprepare(clk);
  888. }
  889. static unsigned long ad7173_sel_clk(struct ad7173_state *st,
  890. unsigned int clk_sel)
  891. {
  892. int ret;
  893. st->adc_mode &= ~AD7173_ADC_MODE_CLOCKSEL_MASK;
  894. st->adc_mode |= FIELD_PREP(AD7173_ADC_MODE_CLOCKSEL_MASK, clk_sel);
  895. ret = ad_sd_write_reg(&st->sd, AD7173_REG_ADC_MODE, 0x2, st->adc_mode);
  896. return ret;
  897. }
  898. static unsigned long ad7173_clk_recalc_rate(struct clk_hw *hw,
  899. unsigned long parent_rate)
  900. {
  901. struct ad7173_state *st = clk_hw_to_ad7173(hw);
  902. return st->info->clock / HZ_PER_KHZ;
  903. }
  904. static int ad7173_clk_output_is_enabled(struct clk_hw *hw)
  905. {
  906. struct ad7173_state *st = clk_hw_to_ad7173(hw);
  907. u32 clk_sel;
  908. clk_sel = FIELD_GET(AD7173_ADC_MODE_CLOCKSEL_MASK, st->adc_mode);
  909. return clk_sel == AD7173_ADC_MODE_CLOCKSEL_INT_OUTPUT;
  910. }
  911. static int ad7173_clk_output_prepare(struct clk_hw *hw)
  912. {
  913. struct ad7173_state *st = clk_hw_to_ad7173(hw);
  914. return ad7173_sel_clk(st, AD7173_ADC_MODE_CLOCKSEL_INT_OUTPUT);
  915. }
  916. static void ad7173_clk_output_unprepare(struct clk_hw *hw)
  917. {
  918. struct ad7173_state *st = clk_hw_to_ad7173(hw);
  919. ad7173_sel_clk(st, AD7173_ADC_MODE_CLOCKSEL_INT);
  920. }
  921. static const struct clk_ops ad7173_int_clk_ops = {
  922. .recalc_rate = ad7173_clk_recalc_rate,
  923. .is_enabled = ad7173_clk_output_is_enabled,
  924. .prepare = ad7173_clk_output_prepare,
  925. .unprepare = ad7173_clk_output_unprepare,
  926. };
  927. static int ad7173_register_clk_provider(struct iio_dev *indio_dev)
  928. {
  929. struct ad7173_state *st = iio_priv(indio_dev);
  930. struct device *dev = indio_dev->dev.parent;
  931. struct fwnode_handle *fwnode = dev_fwnode(dev);
  932. struct clk_init_data init = {};
  933. int ret;
  934. if (!IS_ENABLED(CONFIG_COMMON_CLK))
  935. return 0;
  936. init.name = fwnode_get_name(fwnode);
  937. init.ops = &ad7173_int_clk_ops;
  938. st->int_clk_hw.init = &init;
  939. ret = devm_clk_hw_register(dev, &st->int_clk_hw);
  940. if (ret)
  941. return ret;
  942. return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
  943. &st->int_clk_hw);
  944. }
  945. static int ad4111_validate_current_ain(struct ad7173_state *st,
  946. const unsigned int ain[AD7173_NO_AINS_PER_CHANNEL])
  947. {
  948. struct device *dev = &st->sd.spi->dev;
  949. if (!st->info->has_current_inputs)
  950. return dev_err_probe(dev, -EINVAL,
  951. "Model %s does not support current channels\n",
  952. st->info->name);
  953. if (ain[0] >= ARRAY_SIZE(ad4111_current_channel_config))
  954. return dev_err_probe(dev, -EINVAL,
  955. "For current channels single-channel must be <[0-3]>\n");
  956. return 0;
  957. }
  958. static int ad7173_validate_voltage_ain_inputs(struct ad7173_state *st,
  959. unsigned int ain0, unsigned int ain1)
  960. {
  961. struct device *dev = &st->sd.spi->dev;
  962. bool special_input0, special_input1;
  963. /* (AVDD1-AVSS)/5 power supply monitoring */
  964. if (ain0 == AD7173_AIN_POW_MON_POS && ain1 == AD7173_AIN_POW_MON_NEG &&
  965. st->info->has_pow_supply_monitoring)
  966. return 0;
  967. special_input0 = AD7173_IS_REF_INPUT(ain0) ||
  968. (ain0 == AD4111_VINCOM_INPUT && st->info->has_vincom_input);
  969. special_input1 = AD7173_IS_REF_INPUT(ain1) ||
  970. (ain1 == AD4111_VINCOM_INPUT && st->info->has_vincom_input);
  971. if ((ain0 >= st->info->num_voltage_in && !special_input0) ||
  972. (ain1 >= st->info->num_voltage_in && !special_input1)) {
  973. if (ain0 == AD4111_VINCOM_INPUT || ain1 == AD4111_VINCOM_INPUT)
  974. return dev_err_probe(dev, -EINVAL,
  975. "VINCOM not supported for %s\n", st->info->name);
  976. return dev_err_probe(dev, -EINVAL,
  977. "Input pin number out of range for pair (%d %d).\n",
  978. ain0, ain1);
  979. }
  980. if (AD4111_IS_VINCOM_MISMATCH(ain0, ain1) ||
  981. AD4111_IS_VINCOM_MISMATCH(ain1, ain0))
  982. return dev_err_probe(dev, -EINVAL,
  983. "VINCOM must be paired with inputs having divider.\n");
  984. if (!special_input0 && !special_input1 &&
  985. ((ain0 >= st->info->num_voltage_in_div) !=
  986. (ain1 >= st->info->num_voltage_in_div)))
  987. return dev_err_probe(dev, -EINVAL,
  988. "Both inputs must either have a voltage divider or not have: (%d %d).\n",
  989. ain0, ain1);
  990. return 0;
  991. }
  992. static int ad7173_validate_reference(struct ad7173_state *st, int ref_sel)
  993. {
  994. struct device *dev = &st->sd.spi->dev;
  995. int ret;
  996. if (ref_sel == AD7173_SETUP_REF_SEL_INT_REF && !st->info->has_int_ref)
  997. return dev_err_probe(dev, -EINVAL,
  998. "Internal reference is not available on current model.\n");
  999. if (ref_sel == AD7173_SETUP_REF_SEL_EXT_REF2 && !st->info->has_ref2)
  1000. return dev_err_probe(dev, -EINVAL,
  1001. "External reference 2 is not available on current model.\n");
  1002. ret = ad7173_get_ref_voltage_milli(st, ref_sel);
  1003. if (ret < 0)
  1004. return dev_err_probe(dev, ret, "Cannot use reference %u\n",
  1005. ref_sel);
  1006. return 0;
  1007. }
  1008. static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev)
  1009. {
  1010. struct ad7173_channel *chans_st_arr, *chan_st_priv;
  1011. struct ad7173_state *st = iio_priv(indio_dev);
  1012. struct device *dev = indio_dev->dev.parent;
  1013. struct iio_chan_spec *chan_arr, *chan;
  1014. unsigned int ain[AD7173_NO_AINS_PER_CHANNEL], chan_index = 0;
  1015. int ref_sel, ret, num_channels;
  1016. num_channels = device_get_child_node_count(dev);
  1017. if (st->info->has_temp)
  1018. num_channels++;
  1019. if (num_channels == 0)
  1020. return dev_err_probe(dev, -ENODATA, "No channels specified\n");
  1021. if (num_channels > st->info->num_channels)
  1022. return dev_err_probe(dev, -EINVAL,
  1023. "Too many channels specified. Maximum is %d, not including temperature channel if supported.\n",
  1024. st->info->num_channels);
  1025. indio_dev->num_channels = num_channels;
  1026. st->num_channels = num_channels;
  1027. chan_arr = devm_kcalloc(dev, sizeof(*indio_dev->channels),
  1028. st->num_channels, GFP_KERNEL);
  1029. if (!chan_arr)
  1030. return -ENOMEM;
  1031. chans_st_arr = devm_kcalloc(dev, st->num_channels, sizeof(*st->channels),
  1032. GFP_KERNEL);
  1033. if (!chans_st_arr)
  1034. return -ENOMEM;
  1035. indio_dev->channels = chan_arr;
  1036. st->channels = chans_st_arr;
  1037. if (st->info->has_temp) {
  1038. chan_arr[chan_index] = ad7173_temp_iio_channel_template;
  1039. chan_st_priv = &chans_st_arr[chan_index];
  1040. chan_st_priv->ain =
  1041. AD7173_CH_ADDRESS(chan_arr[chan_index].channel,
  1042. chan_arr[chan_index].channel2);
  1043. chan_st_priv->cfg.bipolar = false;
  1044. chan_st_priv->cfg.input_buf = st->info->has_input_buf;
  1045. chan_st_priv->cfg.ref_sel = AD7173_SETUP_REF_SEL_INT_REF;
  1046. st->adc_mode |= AD7173_ADC_MODE_REF_EN;
  1047. chan_index++;
  1048. }
  1049. device_for_each_child_node_scoped(dev, child) {
  1050. bool is_current_chan = false;
  1051. chan = &chan_arr[chan_index];
  1052. *chan = ad7173_channel_template;
  1053. chan_st_priv = &chans_st_arr[chan_index];
  1054. ret = fwnode_property_read_u32_array(child, "diff-channels",
  1055. ain, ARRAY_SIZE(ain));
  1056. if (ret) {
  1057. ret = fwnode_property_read_u32(child, "single-channel",
  1058. ain);
  1059. if (ret)
  1060. return dev_err_probe(dev, ret,
  1061. "Channel must define one of diff-channels or single-channel.\n");
  1062. is_current_chan = fwnode_property_read_bool(child, "adi,current-channel");
  1063. } else {
  1064. chan->differential = true;
  1065. }
  1066. if (is_current_chan) {
  1067. ret = ad4111_validate_current_ain(st, ain);
  1068. if (ret)
  1069. return ret;
  1070. } else {
  1071. if (!chan->differential) {
  1072. ret = fwnode_property_read_u32(child,
  1073. "common-mode-channel", ain + 1);
  1074. if (ret)
  1075. return dev_err_probe(dev, ret,
  1076. "common-mode-channel must be defined for single-ended channels.\n");
  1077. }
  1078. ret = ad7173_validate_voltage_ain_inputs(st, ain[0], ain[1]);
  1079. if (ret)
  1080. return ret;
  1081. }
  1082. ret = fwnode_property_match_property_string(child,
  1083. "adi,reference-select",
  1084. ad7173_ref_sel_str,
  1085. ARRAY_SIZE(ad7173_ref_sel_str));
  1086. if (ret < 0)
  1087. ref_sel = AD7173_SETUP_REF_SEL_INT_REF;
  1088. else
  1089. ref_sel = ret;
  1090. ret = ad7173_validate_reference(st, ref_sel);
  1091. if (ret)
  1092. return ret;
  1093. if (ref_sel == AD7173_SETUP_REF_SEL_INT_REF)
  1094. st->adc_mode |= AD7173_ADC_MODE_REF_EN;
  1095. chan_st_priv->cfg.ref_sel = ref_sel;
  1096. chan->address = chan_index;
  1097. chan->scan_index = chan_index;
  1098. chan->channel = ain[0];
  1099. chan_st_priv->chan_reg = chan_index;
  1100. chan_st_priv->cfg.input_buf = st->info->has_input_buf;
  1101. chan_st_priv->cfg.odr = 0;
  1102. chan_st_priv->cfg.bipolar = fwnode_property_read_bool(child, "bipolar");
  1103. if (chan_st_priv->cfg.bipolar)
  1104. chan->info_mask_separate |= BIT(IIO_CHAN_INFO_OFFSET);
  1105. if (is_current_chan) {
  1106. chan->type = IIO_CURRENT;
  1107. chan->differential = false;
  1108. chan->channel2 = 0;
  1109. chan_st_priv->ain = ad4111_current_channel_config[ain[0]];
  1110. } else {
  1111. chan_st_priv->cfg.input_buf = st->info->has_input_buf;
  1112. chan->channel2 = ain[1];
  1113. chan_st_priv->ain = AD7173_CH_ADDRESS(ain[0], ain[1]);
  1114. }
  1115. chan_index++;
  1116. }
  1117. return 0;
  1118. }
  1119. static int ad7173_fw_parse_device_config(struct iio_dev *indio_dev)
  1120. {
  1121. struct ad7173_state *st = iio_priv(indio_dev);
  1122. struct device *dev = indio_dev->dev.parent;
  1123. int ret;
  1124. st->regulators[0].supply = ad7173_ref_sel_str[AD7173_SETUP_REF_SEL_EXT_REF];
  1125. st->regulators[1].supply = ad7173_ref_sel_str[AD7173_SETUP_REF_SEL_EXT_REF2];
  1126. st->regulators[2].supply = ad7173_ref_sel_str[AD7173_SETUP_REF_SEL_AVDD1_AVSS];
  1127. /*
  1128. * If a regulator is not available, it will be set to a dummy regulator.
  1129. * Each channel reference is checked with regulator_get_voltage() before
  1130. * setting attributes so if any channel uses a dummy supply the driver
  1131. * probe will fail.
  1132. */
  1133. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(st->regulators),
  1134. st->regulators);
  1135. if (ret)
  1136. return dev_err_probe(dev, ret, "Failed to get regulators\n");
  1137. ret = regulator_bulk_enable(ARRAY_SIZE(st->regulators), st->regulators);
  1138. if (ret)
  1139. return dev_err_probe(dev, ret, "Failed to enable regulators\n");
  1140. ret = devm_add_action_or_reset(dev, ad7173_disable_regulators, st);
  1141. if (ret)
  1142. return dev_err_probe(dev, ret,
  1143. "Failed to add regulators disable action\n");
  1144. ret = device_property_match_property_string(dev, "clock-names",
  1145. ad7173_clk_sel,
  1146. ARRAY_SIZE(ad7173_clk_sel));
  1147. if (ret < 0) {
  1148. st->adc_mode |= FIELD_PREP(AD7173_ADC_MODE_CLOCKSEL_MASK,
  1149. AD7173_ADC_MODE_CLOCKSEL_INT);
  1150. ad7173_register_clk_provider(indio_dev);
  1151. } else {
  1152. st->adc_mode |= FIELD_PREP(AD7173_ADC_MODE_CLOCKSEL_MASK,
  1153. AD7173_ADC_MODE_CLOCKSEL_EXT + ret);
  1154. st->ext_clk = devm_clk_get(dev, ad7173_clk_sel[ret]);
  1155. if (IS_ERR(st->ext_clk))
  1156. return dev_err_probe(dev, PTR_ERR(st->ext_clk),
  1157. "Failed to get external clock\n");
  1158. ret = clk_prepare_enable(st->ext_clk);
  1159. if (ret)
  1160. return dev_err_probe(dev, ret,
  1161. "Failed to enable external clock\n");
  1162. ret = devm_add_action_or_reset(dev, ad7173_clk_disable_unprepare,
  1163. st->ext_clk);
  1164. if (ret)
  1165. return ret;
  1166. }
  1167. ret = fwnode_irq_get_byname(dev_fwnode(dev), "rdy");
  1168. if (ret < 0)
  1169. return dev_err_probe(dev, ret, "Interrupt 'rdy' is required\n");
  1170. st->sigma_delta_info.irq_line = ret;
  1171. return ad7173_fw_parse_channel_config(indio_dev);
  1172. }
  1173. static int ad7173_probe(struct spi_device *spi)
  1174. {
  1175. struct device *dev = &spi->dev;
  1176. struct ad7173_state *st;
  1177. struct iio_dev *indio_dev;
  1178. int ret;
  1179. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  1180. if (!indio_dev)
  1181. return -ENOMEM;
  1182. st = iio_priv(indio_dev);
  1183. st->info = spi_get_device_match_data(spi);
  1184. if (!st->info)
  1185. return -ENODEV;
  1186. ida_init(&st->cfg_slots_status);
  1187. ret = devm_add_action_or_reset(dev, ad7173_ida_destroy, st);
  1188. if (ret)
  1189. return ret;
  1190. indio_dev->name = st->info->name;
  1191. indio_dev->modes = INDIO_DIRECT_MODE;
  1192. indio_dev->info = &ad7173_info;
  1193. spi->mode = SPI_MODE_3;
  1194. spi_setup(spi);
  1195. st->sigma_delta_info = ad7173_sigma_delta_info;
  1196. st->sigma_delta_info.num_slots = st->info->num_configs;
  1197. ret = ad_sd_init(&st->sd, indio_dev, spi, &st->sigma_delta_info);
  1198. if (ret)
  1199. return ret;
  1200. ret = ad7173_fw_parse_device_config(indio_dev);
  1201. if (ret)
  1202. return ret;
  1203. ret = devm_ad_sd_setup_buffer_and_trigger(dev, indio_dev);
  1204. if (ret)
  1205. return ret;
  1206. ret = ad7173_setup(indio_dev);
  1207. if (ret)
  1208. return ret;
  1209. ret = devm_iio_device_register(dev, indio_dev);
  1210. if (ret)
  1211. return ret;
  1212. if (IS_ENABLED(CONFIG_GPIOLIB))
  1213. return ad7173_gpio_init(st);
  1214. return 0;
  1215. }
  1216. static const struct of_device_id ad7173_of_match[] = {
  1217. { .compatible = "adi,ad4111", .data = &ad4111_device_info },
  1218. { .compatible = "adi,ad4112", .data = &ad4112_device_info },
  1219. { .compatible = "adi,ad4114", .data = &ad4114_device_info },
  1220. { .compatible = "adi,ad4115", .data = &ad4115_device_info },
  1221. { .compatible = "adi,ad4116", .data = &ad4116_device_info },
  1222. { .compatible = "adi,ad7172-2", .data = &ad7172_2_device_info },
  1223. { .compatible = "adi,ad7172-4", .data = &ad7172_4_device_info },
  1224. { .compatible = "adi,ad7173-8", .data = &ad7173_8_device_info },
  1225. { .compatible = "adi,ad7175-2", .data = &ad7175_2_device_info },
  1226. { .compatible = "adi,ad7175-8", .data = &ad7175_8_device_info },
  1227. { .compatible = "adi,ad7176-2", .data = &ad7176_2_device_info },
  1228. { .compatible = "adi,ad7177-2", .data = &ad7177_2_device_info },
  1229. { }
  1230. };
  1231. MODULE_DEVICE_TABLE(of, ad7173_of_match);
  1232. static const struct spi_device_id ad7173_id_table[] = {
  1233. { "ad4111", (kernel_ulong_t)&ad4111_device_info },
  1234. { "ad4112", (kernel_ulong_t)&ad4112_device_info },
  1235. { "ad4114", (kernel_ulong_t)&ad4114_device_info },
  1236. { "ad4115", (kernel_ulong_t)&ad4115_device_info },
  1237. { "ad4116", (kernel_ulong_t)&ad4116_device_info },
  1238. { "ad7172-2", (kernel_ulong_t)&ad7172_2_device_info },
  1239. { "ad7172-4", (kernel_ulong_t)&ad7172_4_device_info },
  1240. { "ad7173-8", (kernel_ulong_t)&ad7173_8_device_info },
  1241. { "ad7175-2", (kernel_ulong_t)&ad7175_2_device_info },
  1242. { "ad7175-8", (kernel_ulong_t)&ad7175_8_device_info },
  1243. { "ad7176-2", (kernel_ulong_t)&ad7176_2_device_info },
  1244. { "ad7177-2", (kernel_ulong_t)&ad7177_2_device_info },
  1245. { }
  1246. };
  1247. MODULE_DEVICE_TABLE(spi, ad7173_id_table);
  1248. static struct spi_driver ad7173_driver = {
  1249. .driver = {
  1250. .name = "ad7173",
  1251. .of_match_table = ad7173_of_match,
  1252. },
  1253. .probe = ad7173_probe,
  1254. .id_table = ad7173_id_table,
  1255. };
  1256. module_spi_driver(ad7173_driver);
  1257. MODULE_IMPORT_NS(IIO_AD_SIGMA_DELTA);
  1258. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafo.de>");
  1259. MODULE_AUTHOR("Dumitru Ceclan <dumitru.ceclan@analog.com>");
  1260. MODULE_DESCRIPTION("Analog Devices AD7173 and similar ADC driver");
  1261. MODULE_LICENSE("GPL");