ad4062.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Analog Devices AD4062 I3C ADC driver
  4. *
  5. * Copyright 2025 Analog Devices Inc.
  6. */
  7. #include <linux/array_size.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/bitops.h>
  10. #include <linux/completion.h>
  11. #include <linux/delay.h>
  12. #include <linux/devm-helpers.h>
  13. #include <linux/err.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/i3c/device.h>
  16. #include <linux/i3c/master.h>
  17. #include <linux/iio/buffer.h>
  18. #include <linux/iio/events.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/trigger.h>
  22. #include <linux/iio/trigger_consumer.h>
  23. #include <linux/iio/triggered_buffer.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/math.h>
  27. #include <linux/minmax.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/property.h>
  30. #include <linux/regmap.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/string.h>
  33. #include <linux/types.h>
  34. #include <linux/units.h>
  35. #include <linux/unaligned.h>
  36. #include <linux/util_macros.h>
  37. #define AD4062_REG_INTERFACE_CONFIG_A 0x00
  38. #define AD4062_REG_DEVICE_CONFIG 0x02
  39. #define AD4062_REG_DEVICE_CONFIG_POWER_MODE_MSK GENMASK(1, 0)
  40. #define AD4062_REG_DEVICE_CONFIG_LOW_POWER_MODE 3
  41. #define AD4062_REG_PROD_ID_1 0x05
  42. #define AD4062_REG_DEVICE_GRADE 0x06
  43. #define AD4062_REG_SCRATCH_PAD 0x0A
  44. #define AD4062_REG_VENDOR_H 0x0D
  45. #define AD4062_REG_STREAM_MODE 0x0E
  46. #define AD4062_REG_INTERFACE_STATUS 0x11
  47. #define AD4062_REG_MODE_SET 0x20
  48. #define AD4062_REG_MODE_SET_ENTER_ADC BIT(0)
  49. #define AD4062_REG_ADC_MODES 0x21
  50. #define AD4062_REG_ADC_MODES_MODE_MSK GENMASK(1, 0)
  51. #define AD4062_REG_ADC_CONFIG 0x22
  52. #define AD4062_REG_ADC_CONFIG_REF_EN_MSK BIT(5)
  53. #define AD4062_REG_ADC_CONFIG_SCALE_EN_MSK BIT(4)
  54. #define AD4062_REG_AVG_CONFIG 0x23
  55. #define AD4062_REG_GP_CONF 0x24
  56. #define AD4062_REG_GP_CONF_MODE_MSK_0 GENMASK(2, 0)
  57. #define AD4062_REG_GP_CONF_MODE_MSK_1 GENMASK(6, 4)
  58. #define AD4062_REG_INTR_CONF 0x25
  59. #define AD4062_REG_INTR_CONF_EN_MSK_0 GENMASK(1, 0)
  60. #define AD4062_REG_INTR_CONF_EN_MSK_1 GENMASK(5, 4)
  61. #define AD4062_REG_TIMER_CONFIG 0x27
  62. #define AD4062_REG_TIMER_CONFIG_FS_MASK GENMASK(7, 4)
  63. #define AD4062_REG_MAX_LIMIT 0x29
  64. #define AD4062_REG_MIN_LIMIT 0x2B
  65. #define AD4062_REG_MAX_HYST 0x2C
  66. #define AD4062_REG_MIN_HYST 0x2D
  67. #define AD4062_REG_MON_VAL 0x2F
  68. #define AD4062_REG_ADC_IBI_EN 0x31
  69. #define AD4062_REG_ADC_IBI_EN_CONV_TRIGGER BIT(2)
  70. #define AD4062_REG_ADC_IBI_EN_MAX BIT(1)
  71. #define AD4062_REG_ADC_IBI_EN_MIN BIT(0)
  72. #define AD4062_REG_FUSE_CRC 0x40
  73. #define AD4062_REG_DEVICE_STATUS 0x41
  74. #define AD4062_REG_DEVICE_STATUS_DEVICE_RESET BIT(6)
  75. #define AD4062_REG_IBI_STATUS 0x48
  76. #define AD4062_REG_CONV_READ_LSB 0x50
  77. #define AD4062_REG_CONV_READ_16BITS 0x51
  78. #define AD4062_REG_CONV_READ_32BITS 0x53
  79. #define AD4062_REG_CONV_TRIGGER_16BITS 0x57
  80. #define AD4062_REG_CONV_TRIGGER_32BITS 0x59
  81. #define AD4062_REG_CONV_AUTO 0x61
  82. #define AD4062_MAX_REG AD4062_REG_CONV_AUTO
  83. #define AD4062_MON_VAL_MIDDLE_POINT 0x8000
  84. #define AD4062_I3C_VENDOR 0x0177
  85. #define AD4062_SOFT_RESET 0x81
  86. #define AD4060_PROD_ID 0x7A
  87. #define AD4062_PROD_ID 0x7C
  88. #define AD4062_GP_DISABLED 0x0
  89. #define AD4062_GP_INTR 0x1
  90. #define AD4062_GP_DRDY 0x2
  91. #define AD4062_GP_STATIC_LOW 0x5
  92. #define AD4062_GP_STATIC_HIGH 0x6
  93. #define AD4062_LIMIT_BITS 12
  94. #define AD4062_INTR_EN_NEITHER 0x0
  95. #define AD4062_INTR_EN_EITHER 0x3
  96. #define AD4062_TCONV_NS 270
  97. enum ad4062_operation_mode {
  98. AD4062_SAMPLE_MODE = 0x0,
  99. AD4062_BURST_AVERAGING_MODE = 0x1,
  100. AD4062_MONITOR_MODE = 0x3,
  101. };
  102. struct ad4062_chip_info {
  103. const struct iio_chan_spec channels[1];
  104. const char *name;
  105. u16 prod_id;
  106. u16 avg_max;
  107. };
  108. enum {
  109. AD4062_SCAN_TYPE_SAMPLE,
  110. AD4062_SCAN_TYPE_BURST_AVG,
  111. };
  112. static const struct iio_scan_type ad4062_scan_type_12_s[] = {
  113. [AD4062_SCAN_TYPE_SAMPLE] = {
  114. .sign = 's',
  115. .realbits = 12,
  116. .storagebits = 16,
  117. .endianness = IIO_BE,
  118. },
  119. [AD4062_SCAN_TYPE_BURST_AVG] = {
  120. .sign = 's',
  121. .realbits = 14,
  122. .storagebits = 16,
  123. .endianness = IIO_BE,
  124. },
  125. };
  126. static const struct iio_scan_type ad4062_scan_type_16_s[] = {
  127. [AD4062_SCAN_TYPE_SAMPLE] = {
  128. .sign = 's',
  129. .realbits = 16,
  130. .storagebits = 16,
  131. .endianness = IIO_BE,
  132. },
  133. [AD4062_SCAN_TYPE_BURST_AVG] = {
  134. .sign = 's',
  135. .realbits = 20,
  136. .storagebits = 32,
  137. .endianness = IIO_BE,
  138. },
  139. };
  140. static const unsigned int ad4062_conversion_freqs[] = {
  141. 2000000, 1000000, 300000, 100000, /* 0 - 3 */
  142. 33300, 10000, 3000, 500, /* 4 - 7 */
  143. 333, 250, 200, 166, /* 8 - 11 */
  144. 140, 124, 111, /* 12 - 15 */
  145. };
  146. struct ad4062_state {
  147. const struct ad4062_chip_info *chip;
  148. const struct ad4062_bus_ops *ops;
  149. enum ad4062_operation_mode mode;
  150. struct work_struct trig_conv;
  151. struct completion completion;
  152. struct iio_trigger *trigger;
  153. struct iio_dev *indio_dev;
  154. struct i3c_device *i3cdev;
  155. struct regmap *regmap;
  156. bool wait_event;
  157. int vref_uV;
  158. unsigned int samp_freqs[ARRAY_SIZE(ad4062_conversion_freqs)];
  159. bool gpo_irq[2];
  160. u16 sampling_frequency;
  161. u16 events_frequency;
  162. u8 oversamp_ratio;
  163. u8 conv_sizeof;
  164. u8 conv_addr;
  165. union {
  166. __be32 be32;
  167. __be16 be16;
  168. } buf __aligned(IIO_DMA_MINALIGN);
  169. };
  170. static const struct regmap_range ad4062_regmap_rd_ranges[] = {
  171. regmap_reg_range(AD4062_REG_INTERFACE_CONFIG_A, AD4062_REG_DEVICE_GRADE),
  172. regmap_reg_range(AD4062_REG_SCRATCH_PAD, AD4062_REG_INTERFACE_STATUS),
  173. regmap_reg_range(AD4062_REG_MODE_SET, AD4062_REG_ADC_IBI_EN),
  174. regmap_reg_range(AD4062_REG_FUSE_CRC, AD4062_REG_IBI_STATUS),
  175. regmap_reg_range(AD4062_REG_CONV_READ_LSB, AD4062_REG_CONV_AUTO),
  176. };
  177. static const struct regmap_access_table ad4062_regmap_rd_table = {
  178. .yes_ranges = ad4062_regmap_rd_ranges,
  179. .n_yes_ranges = ARRAY_SIZE(ad4062_regmap_rd_ranges),
  180. };
  181. static const struct regmap_range ad4062_regmap_wr_ranges[] = {
  182. regmap_reg_range(AD4062_REG_INTERFACE_CONFIG_A, AD4062_REG_DEVICE_CONFIG),
  183. regmap_reg_range(AD4062_REG_SCRATCH_PAD, AD4062_REG_SCRATCH_PAD),
  184. regmap_reg_range(AD4062_REG_STREAM_MODE, AD4062_REG_INTERFACE_STATUS),
  185. regmap_reg_range(AD4062_REG_MODE_SET, AD4062_REG_ADC_IBI_EN),
  186. regmap_reg_range(AD4062_REG_FUSE_CRC, AD4062_REG_DEVICE_STATUS),
  187. };
  188. static const struct regmap_access_table ad4062_regmap_wr_table = {
  189. .yes_ranges = ad4062_regmap_wr_ranges,
  190. .n_yes_ranges = ARRAY_SIZE(ad4062_regmap_wr_ranges),
  191. };
  192. static const struct iio_event_spec ad4062_events[] = {
  193. {
  194. .type = IIO_EV_TYPE_THRESH,
  195. .dir = IIO_EV_DIR_EITHER,
  196. .mask_shared_by_all = BIT(IIO_EV_INFO_ENABLE),
  197. },
  198. {
  199. .type = IIO_EV_TYPE_THRESH,
  200. .dir = IIO_EV_DIR_RISING,
  201. .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE) |
  202. BIT(IIO_EV_INFO_HYSTERESIS),
  203. },
  204. {
  205. .type = IIO_EV_TYPE_THRESH,
  206. .dir = IIO_EV_DIR_FALLING,
  207. .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE) |
  208. BIT(IIO_EV_INFO_HYSTERESIS),
  209. },
  210. };
  211. #define AD4062_CHAN(bits) { \
  212. .type = IIO_VOLTAGE, \
  213. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_RAW) | \
  214. BIT(IIO_CHAN_INFO_SCALE) | \
  215. BIT(IIO_CHAN_INFO_CALIBSCALE) | \
  216. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  217. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  218. .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  219. .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  220. .indexed = 1, \
  221. .channel = 0, \
  222. .event_spec = ad4062_events, \
  223. .num_event_specs = ARRAY_SIZE(ad4062_events), \
  224. .has_ext_scan_type = 1, \
  225. .ext_scan_type = ad4062_scan_type_##bits##_s, \
  226. .num_ext_scan_type = ARRAY_SIZE(ad4062_scan_type_##bits##_s), \
  227. }
  228. static const struct ad4062_chip_info ad4060_chip_info = {
  229. .name = "ad4060",
  230. .channels = { AD4062_CHAN(12) },
  231. .prod_id = AD4060_PROD_ID,
  232. .avg_max = 256,
  233. };
  234. static const struct ad4062_chip_info ad4062_chip_info = {
  235. .name = "ad4062",
  236. .channels = { AD4062_CHAN(16) },
  237. .prod_id = AD4062_PROD_ID,
  238. .avg_max = 4096,
  239. };
  240. static ssize_t sampling_frequency_show(struct device *dev,
  241. struct device_attribute *attr, char *buf)
  242. {
  243. struct ad4062_state *st = iio_priv(dev_to_iio_dev(dev));
  244. return sysfs_emit(buf, "%d\n", ad4062_conversion_freqs[st->events_frequency]);
  245. }
  246. static int sampling_frequency_store_dispatch(struct iio_dev *indio_dev,
  247. const char *buf)
  248. {
  249. struct ad4062_state *st = iio_priv(indio_dev);
  250. int val, ret;
  251. if (st->wait_event)
  252. return -EBUSY;
  253. ret = kstrtoint(buf, 10, &val);
  254. if (ret)
  255. return ret;
  256. st->events_frequency = find_closest_descending(val, ad4062_conversion_freqs,
  257. ARRAY_SIZE(ad4062_conversion_freqs));
  258. return 0;
  259. }
  260. static ssize_t sampling_frequency_store(struct device *dev,
  261. struct device_attribute *attr,
  262. const char *buf, size_t len)
  263. {
  264. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  265. int ret;
  266. if (!iio_device_claim_direct(indio_dev))
  267. return -EBUSY;
  268. ret = sampling_frequency_store_dispatch(indio_dev, buf);
  269. iio_device_release_direct(indio_dev);
  270. return ret ?: len;
  271. }
  272. static IIO_DEVICE_ATTR_RW(sampling_frequency, 0);
  273. static ssize_t sampling_frequency_available_show(struct device *dev,
  274. struct device_attribute *attr,
  275. char *buf)
  276. {
  277. int ret = 0;
  278. for (u8 i = 0; i < ARRAY_SIZE(ad4062_conversion_freqs); i++)
  279. ret += sysfs_emit_at(buf, ret, "%d%s", ad4062_conversion_freqs[i],
  280. i != (ARRAY_SIZE(ad4062_conversion_freqs) - 1) ? " " : "\n");
  281. return ret;
  282. }
  283. static IIO_DEVICE_ATTR_RO(sampling_frequency_available, 0);
  284. static struct attribute *ad4062_event_attributes[] = {
  285. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  286. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  287. NULL
  288. };
  289. static const struct attribute_group ad4062_event_attribute_group = {
  290. .attrs = ad4062_event_attributes,
  291. };
  292. static int ad4062_set_oversampling_ratio(struct ad4062_state *st, int val, int val2)
  293. {
  294. const u32 _max = st->chip->avg_max;
  295. const u32 _min = 1;
  296. int ret;
  297. if (!in_range(val, _min, _max) || val2 != 0)
  298. return -EINVAL;
  299. /* 1 disables oversampling */
  300. val = ilog2(val);
  301. if (val == 0) {
  302. st->mode = AD4062_SAMPLE_MODE;
  303. } else {
  304. st->mode = AD4062_BURST_AVERAGING_MODE;
  305. ret = regmap_write(st->regmap, AD4062_REG_AVG_CONFIG, val - 1);
  306. if (ret)
  307. return ret;
  308. }
  309. st->oversamp_ratio = val;
  310. return 0;
  311. }
  312. static int ad4062_get_oversampling_ratio(struct ad4062_state *st, int *val)
  313. {
  314. int ret, buf;
  315. if (st->mode == AD4062_SAMPLE_MODE) {
  316. *val = 1;
  317. return 0;
  318. }
  319. ret = regmap_read(st->regmap, AD4062_REG_AVG_CONFIG, &buf);
  320. if (ret)
  321. return ret;
  322. *val = BIT(buf + 1);
  323. return 0;
  324. }
  325. static int ad4062_calc_sampling_frequency(unsigned int fosc, unsigned int oversamp_ratio)
  326. {
  327. /* From datasheet p.31: (n_avg - 1)/fosc + tconv */
  328. u32 n_avg = BIT(oversamp_ratio) - 1;
  329. u32 period_ns = NSEC_PER_SEC / fosc;
  330. /* Result is less than 1 Hz */
  331. if (n_avg >= fosc)
  332. return 1;
  333. return NSEC_PER_SEC / (n_avg * period_ns + AD4062_TCONV_NS);
  334. }
  335. static int ad4062_populate_sampling_frequency(struct ad4062_state *st)
  336. {
  337. for (u8 i = 0; i < ARRAY_SIZE(ad4062_conversion_freqs); i++)
  338. st->samp_freqs[i] =
  339. ad4062_calc_sampling_frequency(ad4062_conversion_freqs[i],
  340. st->oversamp_ratio);
  341. return 0;
  342. }
  343. static int ad4062_get_sampling_frequency(struct ad4062_state *st, int *val)
  344. {
  345. int freq = ad4062_conversion_freqs[st->sampling_frequency];
  346. *val = ad4062_calc_sampling_frequency(freq, st->oversamp_ratio);
  347. return IIO_VAL_INT;
  348. }
  349. static int ad4062_set_sampling_frequency(struct ad4062_state *st, int val, int val2)
  350. {
  351. int ret;
  352. if (val2 != 0)
  353. return -EINVAL;
  354. ret = ad4062_populate_sampling_frequency(st);
  355. if (ret)
  356. return ret;
  357. st->sampling_frequency =
  358. find_closest_descending(val, st->samp_freqs,
  359. ARRAY_SIZE(ad4062_conversion_freqs));
  360. return 0;
  361. }
  362. static int ad4062_check_ids(struct ad4062_state *st)
  363. {
  364. struct device *dev = &st->i3cdev->dev;
  365. int ret;
  366. u16 val;
  367. ret = regmap_bulk_read(st->regmap, AD4062_REG_PROD_ID_1,
  368. &st->buf.be16, sizeof(st->buf.be16));
  369. if (ret)
  370. return ret;
  371. val = be16_to_cpu(st->buf.be16);
  372. if (val != st->chip->prod_id)
  373. dev_warn(dev, "Production ID x%x does not match known values", val);
  374. ret = regmap_bulk_read(st->regmap, AD4062_REG_VENDOR_H,
  375. &st->buf.be16, sizeof(st->buf.be16));
  376. if (ret)
  377. return ret;
  378. val = be16_to_cpu(st->buf.be16);
  379. if (val != AD4062_I3C_VENDOR) {
  380. dev_err(dev, "Vendor ID x%x does not match expected value\n", val);
  381. return -ENODEV;
  382. }
  383. return 0;
  384. }
  385. static int ad4062_conversion_frequency_set(struct ad4062_state *st, u8 val)
  386. {
  387. return regmap_write(st->regmap, AD4062_REG_TIMER_CONFIG,
  388. FIELD_PREP(AD4062_REG_TIMER_CONFIG_FS_MASK, val));
  389. }
  390. static int ad4062_set_operation_mode(struct ad4062_state *st,
  391. enum ad4062_operation_mode mode)
  392. {
  393. const unsigned int samp_freq = mode == AD4062_MONITOR_MODE ?
  394. st->events_frequency : st->sampling_frequency;
  395. int ret;
  396. ret = ad4062_conversion_frequency_set(st, samp_freq);
  397. if (ret)
  398. return ret;
  399. ret = regmap_update_bits(st->regmap, AD4062_REG_ADC_MODES,
  400. AD4062_REG_ADC_MODES_MODE_MSK, mode);
  401. if (ret)
  402. return ret;
  403. if (mode == AD4062_MONITOR_MODE) {
  404. /* Change address pointer to enter monitor mode */
  405. struct i3c_xfer xfer_trigger = {
  406. .data.out = &st->conv_addr,
  407. .len = sizeof(st->conv_addr),
  408. .rnw = false,
  409. };
  410. st->conv_addr = AD4062_REG_CONV_TRIGGER_32BITS;
  411. return i3c_device_do_xfers(st->i3cdev, &xfer_trigger, 1, I3C_SDR);
  412. }
  413. return regmap_write(st->regmap, AD4062_REG_MODE_SET,
  414. AD4062_REG_MODE_SET_ENTER_ADC);
  415. }
  416. static int ad4062_soft_reset(struct ad4062_state *st)
  417. {
  418. u8 val = AD4062_SOFT_RESET;
  419. int ret;
  420. ret = regmap_write(st->regmap, AD4062_REG_INTERFACE_CONFIG_A, val);
  421. if (ret)
  422. return ret;
  423. /* Wait AD4062 treset time, datasheet p8 */
  424. ndelay(60);
  425. return 0;
  426. }
  427. static int ad4062_setup(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
  428. const bool *ref_sel)
  429. {
  430. struct ad4062_state *st = iio_priv(indio_dev);
  431. const struct iio_scan_type *scan_type;
  432. int ret;
  433. scan_type = iio_get_current_scan_type(indio_dev, chan);
  434. if (IS_ERR(scan_type))
  435. return PTR_ERR(scan_type);
  436. ret = regmap_update_bits(st->regmap, AD4062_REG_GP_CONF,
  437. AD4062_REG_GP_CONF_MODE_MSK_0,
  438. FIELD_PREP(AD4062_REG_GP_CONF_MODE_MSK_0,
  439. AD4062_GP_INTR));
  440. if (ret)
  441. return ret;
  442. ret = regmap_update_bits(st->regmap, AD4062_REG_GP_CONF,
  443. AD4062_REG_GP_CONF_MODE_MSK_1,
  444. FIELD_PREP(AD4062_REG_GP_CONF_MODE_MSK_1,
  445. AD4062_GP_DRDY));
  446. if (ret)
  447. return ret;
  448. ret = regmap_update_bits(st->regmap, AD4062_REG_ADC_CONFIG,
  449. AD4062_REG_ADC_CONFIG_REF_EN_MSK,
  450. FIELD_PREP(AD4062_REG_ADC_CONFIG_REF_EN_MSK,
  451. *ref_sel));
  452. if (ret)
  453. return ret;
  454. ret = regmap_write(st->regmap, AD4062_REG_DEVICE_STATUS,
  455. AD4062_REG_DEVICE_STATUS_DEVICE_RESET);
  456. if (ret)
  457. return ret;
  458. ret = regmap_update_bits(st->regmap, AD4062_REG_INTR_CONF,
  459. AD4062_REG_INTR_CONF_EN_MSK_0,
  460. FIELD_PREP(AD4062_REG_INTR_CONF_EN_MSK_0,
  461. AD4062_INTR_EN_EITHER));
  462. if (ret)
  463. return ret;
  464. ret = regmap_update_bits(st->regmap, AD4062_REG_INTR_CONF,
  465. AD4062_REG_INTR_CONF_EN_MSK_1,
  466. FIELD_PREP(AD4062_REG_INTR_CONF_EN_MSK_1,
  467. AD4062_INTR_EN_NEITHER));
  468. if (ret)
  469. return ret;
  470. st->buf.be16 = cpu_to_be16(AD4062_MON_VAL_MIDDLE_POINT);
  471. return regmap_bulk_write(st->regmap, AD4062_REG_MON_VAL,
  472. &st->buf.be16, sizeof(st->buf.be16));
  473. }
  474. static irqreturn_t ad4062_irq_handler_thresh(int irq, void *private)
  475. {
  476. struct iio_dev *indio_dev = private;
  477. iio_push_event(indio_dev,
  478. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
  479. IIO_EV_TYPE_THRESH,
  480. IIO_EV_DIR_EITHER),
  481. iio_get_time_ns(indio_dev));
  482. return IRQ_HANDLED;
  483. }
  484. static irqreturn_t ad4062_irq_handler_drdy(int irq, void *private)
  485. {
  486. struct iio_dev *indio_dev = private;
  487. struct ad4062_state *st = iio_priv(indio_dev);
  488. if (iio_buffer_enabled(indio_dev) && iio_trigger_using_own(indio_dev))
  489. iio_trigger_poll(st->trigger);
  490. else
  491. complete(&st->completion);
  492. return IRQ_HANDLED;
  493. }
  494. static void ad4062_ibi_handler(struct i3c_device *i3cdev,
  495. const struct i3c_ibi_payload *payload)
  496. {
  497. struct ad4062_state *st = i3cdev_get_drvdata(i3cdev);
  498. if (st->wait_event) {
  499. iio_push_event(st->indio_dev,
  500. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
  501. IIO_EV_TYPE_THRESH,
  502. IIO_EV_DIR_EITHER),
  503. iio_get_time_ns(st->indio_dev));
  504. return;
  505. }
  506. if (iio_buffer_enabled(st->indio_dev))
  507. iio_trigger_poll_nested(st->trigger);
  508. else
  509. complete(&st->completion);
  510. }
  511. static void ad4062_trigger_work(struct work_struct *work)
  512. {
  513. struct ad4062_state *st =
  514. container_of(work, struct ad4062_state, trig_conv);
  515. int ret;
  516. /*
  517. * Read current conversion, if at reg CONV_READ, stop bit triggers
  518. * next sample and does not need writing the address.
  519. */
  520. struct i3c_xfer xfer_sample = {
  521. .data.in = &st->buf.be32,
  522. .len = st->conv_sizeof,
  523. .rnw = true,
  524. };
  525. struct i3c_xfer xfer_trigger = {
  526. .data.out = &st->conv_addr,
  527. .len = sizeof(st->conv_addr),
  528. .rnw = false,
  529. };
  530. ret = i3c_device_do_xfers(st->i3cdev, &xfer_sample, 1, I3C_SDR);
  531. if (ret)
  532. return;
  533. iio_push_to_buffers_with_ts(st->indio_dev, &st->buf.be32, st->conv_sizeof,
  534. iio_get_time_ns(st->indio_dev));
  535. if (st->gpo_irq[1])
  536. return;
  537. i3c_device_do_xfers(st->i3cdev, &xfer_trigger, 1, I3C_SDR);
  538. }
  539. static irqreturn_t ad4062_poll_handler(int irq, void *p)
  540. {
  541. struct iio_poll_func *pf = p;
  542. struct iio_dev *indio_dev = pf->indio_dev;
  543. struct ad4062_state *st = iio_priv(indio_dev);
  544. iio_trigger_notify_done(indio_dev->trig);
  545. schedule_work(&st->trig_conv);
  546. return IRQ_HANDLED;
  547. }
  548. static void ad4062_disable_ibi(void *data)
  549. {
  550. struct i3c_device *i3cdev = data;
  551. i3c_device_disable_ibi(i3cdev);
  552. }
  553. static void ad4062_free_ibi(void *data)
  554. {
  555. struct i3c_device *i3cdev = data;
  556. i3c_device_free_ibi(i3cdev);
  557. }
  558. static int ad4062_request_ibi(struct i3c_device *i3cdev)
  559. {
  560. const struct i3c_ibi_setup ibireq = {
  561. .max_payload_len = 1,
  562. .num_slots = 1,
  563. .handler = ad4062_ibi_handler,
  564. };
  565. int ret;
  566. ret = i3c_device_request_ibi(i3cdev, &ibireq);
  567. if (ret)
  568. return ret;
  569. ret = devm_add_action_or_reset(&i3cdev->dev, ad4062_free_ibi, i3cdev);
  570. if (ret)
  571. return ret;
  572. ret = i3c_device_enable_ibi(i3cdev);
  573. if (ret)
  574. return ret;
  575. return devm_add_action_or_reset(&i3cdev->dev, ad4062_disable_ibi, i3cdev);
  576. }
  577. static int ad4062_request_irq(struct iio_dev *indio_dev)
  578. {
  579. struct ad4062_state *st = iio_priv(indio_dev);
  580. struct device *dev = &st->i3cdev->dev;
  581. int ret;
  582. ret = fwnode_irq_get_byname(dev_fwnode(&st->i3cdev->dev), "gp0");
  583. if (ret == -EPROBE_DEFER)
  584. return ret;
  585. if (ret < 0) {
  586. st->gpo_irq[0] = false;
  587. ret = regmap_update_bits(st->regmap, AD4062_REG_ADC_IBI_EN,
  588. AD4062_REG_ADC_IBI_EN_MAX | AD4062_REG_ADC_IBI_EN_MIN,
  589. AD4062_REG_ADC_IBI_EN_MAX | AD4062_REG_ADC_IBI_EN_MIN);
  590. if (ret)
  591. return ret;
  592. } else {
  593. st->gpo_irq[0] = true;
  594. ret = devm_request_threaded_irq(dev, ret, NULL,
  595. ad4062_irq_handler_thresh,
  596. IRQF_ONESHOT, indio_dev->name,
  597. indio_dev);
  598. if (ret)
  599. return ret;
  600. }
  601. ret = fwnode_irq_get_byname(dev_fwnode(&st->i3cdev->dev), "gp1");
  602. if (ret == -EPROBE_DEFER)
  603. return ret;
  604. if (ret < 0) {
  605. st->gpo_irq[1] = false;
  606. return regmap_update_bits(st->regmap, AD4062_REG_ADC_IBI_EN,
  607. AD4062_REG_ADC_IBI_EN_CONV_TRIGGER,
  608. AD4062_REG_ADC_IBI_EN_CONV_TRIGGER);
  609. }
  610. st->gpo_irq[1] = true;
  611. return devm_request_irq(dev, ret, ad4062_irq_handler_drdy,
  612. IRQF_NO_THREAD, indio_dev->name, indio_dev);
  613. }
  614. static const struct iio_trigger_ops ad4062_trigger_ops = {
  615. .validate_device = &iio_trigger_validate_own_device,
  616. };
  617. static int ad4062_request_trigger(struct iio_dev *indio_dev)
  618. {
  619. struct ad4062_state *st = iio_priv(indio_dev);
  620. struct device *dev = &st->i3cdev->dev;
  621. int ret;
  622. st->trigger = devm_iio_trigger_alloc(dev, "%s-dev%d",
  623. indio_dev->name,
  624. iio_device_id(indio_dev));
  625. if (!st->trigger)
  626. return -ENOMEM;
  627. st->trigger->ops = &ad4062_trigger_ops;
  628. iio_trigger_set_drvdata(st->trigger, indio_dev);
  629. ret = devm_iio_trigger_register(dev, st->trigger);
  630. if (ret)
  631. return ret;
  632. indio_dev->trig = iio_trigger_get(st->trigger);
  633. return 0;
  634. }
  635. static const int ad4062_oversampling_avail[] = {
  636. 1, 2, 4, 8, 16, 32, 64, 128, /* 0 - 7 */
  637. 256, 512, 1024, 2048, 4096, /* 8 - 12 */
  638. };
  639. static int ad4062_read_avail(struct iio_dev *indio_dev,
  640. struct iio_chan_spec const *chan, const int **vals,
  641. int *type, int *len, long mask)
  642. {
  643. struct ad4062_state *st = iio_priv(indio_dev);
  644. int ret;
  645. switch (mask) {
  646. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  647. *vals = ad4062_oversampling_avail;
  648. *len = ARRAY_SIZE(ad4062_oversampling_avail);
  649. *len -= st->chip->avg_max == 256 ? 4 : 0;
  650. *type = IIO_VAL_INT;
  651. return IIO_AVAIL_LIST;
  652. case IIO_CHAN_INFO_SAMP_FREQ:
  653. ret = ad4062_populate_sampling_frequency(st);
  654. if (ret)
  655. return ret;
  656. *vals = st->samp_freqs;
  657. *len = st->oversamp_ratio ? ARRAY_SIZE(ad4062_conversion_freqs) : 1;
  658. *type = IIO_VAL_INT;
  659. return IIO_AVAIL_LIST;
  660. default:
  661. return -EINVAL;
  662. }
  663. }
  664. static int ad4062_get_chan_scale(struct iio_dev *indio_dev, int *val, int *val2)
  665. {
  666. struct ad4062_state *st = iio_priv(indio_dev);
  667. const struct iio_scan_type *scan_type;
  668. /*
  669. * In burst averaging mode the averaging filter accumulates resulting
  670. * in a sample with increased precision.
  671. */
  672. scan_type = iio_get_current_scan_type(indio_dev, st->chip->channels);
  673. if (IS_ERR(scan_type))
  674. return PTR_ERR(scan_type);
  675. *val = (st->vref_uV * 2) / (MICRO / MILLI); /* signed */
  676. *val2 = scan_type->realbits - 1;
  677. return IIO_VAL_FRACTIONAL_LOG2;
  678. }
  679. static int ad4062_get_chan_calibscale(struct ad4062_state *st, int *val, int *val2)
  680. {
  681. int ret;
  682. ret = regmap_bulk_read(st->regmap, AD4062_REG_MON_VAL,
  683. &st->buf.be16, sizeof(st->buf.be16));
  684. if (ret)
  685. return ret;
  686. /* From datasheet: code out = code in × mon_val/0x8000 */
  687. *val = be16_to_cpu(st->buf.be16) * 2;
  688. *val2 = 16;
  689. return IIO_VAL_FRACTIONAL_LOG2;
  690. }
  691. static int ad4062_set_chan_calibscale(struct ad4062_state *st, int gain_int,
  692. int gain_frac)
  693. {
  694. /* Divide numerator and denumerator by known great common divider */
  695. const u32 mon_val = AD4062_MON_VAL_MIDDLE_POINT / 64;
  696. const u32 micro = MICRO / 64;
  697. const u32 gain_fp = gain_int * MICRO + gain_frac;
  698. const u32 reg_val = DIV_ROUND_CLOSEST(gain_fp * mon_val, micro);
  699. int ret;
  700. /* Checks if the gain is in range and the value fits the field */
  701. if (gain_int < 0 || gain_int > 1 || reg_val > BIT(16) - 1)
  702. return -EINVAL;
  703. st->buf.be16 = cpu_to_be16(reg_val);
  704. ret = regmap_bulk_write(st->regmap, AD4062_REG_MON_VAL,
  705. &st->buf.be16, sizeof(st->buf.be16));
  706. if (ret)
  707. return ret;
  708. /* Enable scale if gain is not equal to one */
  709. return regmap_update_bits(st->regmap, AD4062_REG_ADC_CONFIG,
  710. AD4062_REG_ADC_CONFIG_SCALE_EN_MSK,
  711. FIELD_PREP(AD4062_REG_ADC_CONFIG_SCALE_EN_MSK,
  712. !(gain_int == 1 && gain_frac == 0)));
  713. }
  714. static int ad4062_read_chan_raw(struct ad4062_state *st, int *val)
  715. {
  716. struct i3c_device *i3cdev = st->i3cdev;
  717. struct i3c_xfer xfer_trigger = {
  718. .data.out = &st->conv_addr,
  719. .len = sizeof(st->conv_addr),
  720. .rnw = false,
  721. };
  722. struct i3c_xfer xfer_sample = {
  723. .data.in = &st->buf.be32,
  724. .len = sizeof(st->buf.be32),
  725. .rnw = true,
  726. };
  727. int ret;
  728. PM_RUNTIME_ACQUIRE(&st->i3cdev->dev, pm);
  729. ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
  730. if (ret)
  731. return ret;
  732. ret = ad4062_set_operation_mode(st, st->mode);
  733. if (ret)
  734. return ret;
  735. reinit_completion(&st->completion);
  736. /* Change address pointer to trigger conversion */
  737. st->conv_addr = AD4062_REG_CONV_TRIGGER_32BITS;
  738. ret = i3c_device_do_xfers(i3cdev, &xfer_trigger, 1, I3C_SDR);
  739. if (ret)
  740. return ret;
  741. /*
  742. * Single sample read should be used only for oversampling and
  743. * sampling frequency pairs that take less than 1 sec.
  744. */
  745. ret = wait_for_completion_timeout(&st->completion,
  746. msecs_to_jiffies(1000));
  747. if (!ret)
  748. return -ETIMEDOUT;
  749. ret = i3c_device_do_xfers(i3cdev, &xfer_sample, 1, I3C_SDR);
  750. if (ret)
  751. return ret;
  752. *val = be32_to_cpu(st->buf.be32);
  753. return 0;
  754. }
  755. static int ad4062_read_raw_dispatch(struct ad4062_state *st,
  756. int *val, int *val2, long info)
  757. {
  758. if (st->wait_event)
  759. return -EBUSY;
  760. switch (info) {
  761. case IIO_CHAN_INFO_RAW:
  762. return ad4062_read_chan_raw(st, val);
  763. case IIO_CHAN_INFO_CALIBSCALE:
  764. return ad4062_get_chan_calibscale(st, val, val2);
  765. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  766. return ad4062_get_oversampling_ratio(st, val);
  767. default:
  768. return -EINVAL;
  769. }
  770. }
  771. static int ad4062_read_raw(struct iio_dev *indio_dev,
  772. struct iio_chan_spec const *chan,
  773. int *val, int *val2, long info)
  774. {
  775. struct ad4062_state *st = iio_priv(indio_dev);
  776. int ret;
  777. switch (info) {
  778. case IIO_CHAN_INFO_SCALE:
  779. return ad4062_get_chan_scale(indio_dev, val, val2);
  780. case IIO_CHAN_INFO_SAMP_FREQ:
  781. return ad4062_get_sampling_frequency(st, val);
  782. }
  783. if (!iio_device_claim_direct(indio_dev))
  784. return -EBUSY;
  785. ret = ad4062_read_raw_dispatch(st, val, val2, info);
  786. iio_device_release_direct(indio_dev);
  787. return ret ?: IIO_VAL_INT;
  788. }
  789. static int ad4062_write_raw_dispatch(struct ad4062_state *st, int val, int val2,
  790. long info)
  791. {
  792. if (st->wait_event)
  793. return -EBUSY;
  794. switch (info) {
  795. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  796. return ad4062_set_oversampling_ratio(st, val, val2);
  797. case IIO_CHAN_INFO_CALIBSCALE:
  798. return ad4062_set_chan_calibscale(st, val, val2);
  799. default:
  800. return -EINVAL;
  801. }
  802. }
  803. static int ad4062_write_raw(struct iio_dev *indio_dev,
  804. struct iio_chan_spec const *chan, int val,
  805. int val2, long info)
  806. {
  807. struct ad4062_state *st = iio_priv(indio_dev);
  808. int ret;
  809. switch (info) {
  810. case IIO_CHAN_INFO_SAMP_FREQ:
  811. return ad4062_set_sampling_frequency(st, val, val2);
  812. }
  813. if (!iio_device_claim_direct(indio_dev))
  814. return -EBUSY;
  815. ret = ad4062_write_raw_dispatch(st, val, val2, info);
  816. iio_device_release_direct(indio_dev);
  817. return ret;
  818. }
  819. static int pm_ad4062_monitor_mode_enable(struct ad4062_state *st)
  820. {
  821. int ret;
  822. PM_RUNTIME_ACQUIRE(&st->i3cdev->dev, pm);
  823. ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
  824. if (ret)
  825. return ret;
  826. return ad4062_set_operation_mode(st, AD4062_MONITOR_MODE);
  827. }
  828. static int ad4062_monitor_mode_enable(struct ad4062_state *st)
  829. {
  830. int ret;
  831. ret = pm_ad4062_monitor_mode_enable(st);
  832. if (ret)
  833. return ret;
  834. pm_runtime_get_noresume(&st->i3cdev->dev);
  835. return 0;
  836. }
  837. static int ad4062_monitor_mode_disable(struct ad4062_state *st)
  838. {
  839. pm_runtime_put_autosuspend(&st->i3cdev->dev);
  840. return 0;
  841. }
  842. static int ad4062_read_event_config(struct iio_dev *indio_dev,
  843. const struct iio_chan_spec *chan,
  844. enum iio_event_type type,
  845. enum iio_event_direction dir)
  846. {
  847. struct ad4062_state *st = iio_priv(indio_dev);
  848. return st->wait_event;
  849. }
  850. static int ad4062_write_event_config_dispatch(struct iio_dev *indio_dev,
  851. bool state)
  852. {
  853. struct ad4062_state *st = iio_priv(indio_dev);
  854. int ret;
  855. if (st->wait_event == state)
  856. ret = 0;
  857. else if (state)
  858. ret = ad4062_monitor_mode_enable(st);
  859. else
  860. ret = ad4062_monitor_mode_disable(st);
  861. if (ret)
  862. return ret;
  863. st->wait_event = state;
  864. return 0;
  865. }
  866. static int ad4062_write_event_config(struct iio_dev *indio_dev,
  867. const struct iio_chan_spec *chan,
  868. enum iio_event_type type,
  869. enum iio_event_direction dir,
  870. bool state)
  871. {
  872. int ret;
  873. if (!iio_device_claim_direct(indio_dev))
  874. return -EBUSY;
  875. ret = ad4062_write_event_config_dispatch(indio_dev, state);
  876. iio_device_release_direct(indio_dev);
  877. return ret;
  878. }
  879. static int __ad4062_read_event_info_value(struct ad4062_state *st,
  880. enum iio_event_direction dir, int *val)
  881. {
  882. int ret;
  883. u8 reg;
  884. if (dir == IIO_EV_DIR_RISING)
  885. reg = AD4062_REG_MAX_LIMIT;
  886. else
  887. reg = AD4062_REG_MIN_LIMIT;
  888. ret = regmap_bulk_read(st->regmap, reg, &st->buf.be16,
  889. sizeof(st->buf.be16));
  890. if (ret)
  891. return ret;
  892. *val = sign_extend32(be16_to_cpu(st->buf.be16), AD4062_LIMIT_BITS - 1);
  893. return 0;
  894. }
  895. static int __ad4062_read_event_info_hysteresis(struct ad4062_state *st,
  896. enum iio_event_direction dir, int *val)
  897. {
  898. u8 reg;
  899. if (dir == IIO_EV_DIR_RISING)
  900. reg = AD4062_REG_MAX_HYST;
  901. else
  902. reg = AD4062_REG_MIN_HYST;
  903. return regmap_read(st->regmap, reg, val);
  904. }
  905. static int ad4062_read_event_config_dispatch(struct iio_dev *indio_dev,
  906. enum iio_event_direction dir,
  907. enum iio_event_info info, int *val)
  908. {
  909. struct ad4062_state *st = iio_priv(indio_dev);
  910. if (st->wait_event)
  911. return -EBUSY;
  912. switch (info) {
  913. case IIO_EV_INFO_VALUE:
  914. return __ad4062_read_event_info_value(st, dir, val);
  915. case IIO_EV_INFO_HYSTERESIS:
  916. return __ad4062_read_event_info_hysteresis(st, dir, val);
  917. default:
  918. return -EINVAL;
  919. }
  920. }
  921. static int ad4062_read_event_value(struct iio_dev *indio_dev,
  922. const struct iio_chan_spec *chan,
  923. enum iio_event_type type,
  924. enum iio_event_direction dir,
  925. enum iio_event_info info, int *val,
  926. int *val2)
  927. {
  928. int ret;
  929. if (!iio_device_claim_direct(indio_dev))
  930. return -EBUSY;
  931. ret = ad4062_read_event_config_dispatch(indio_dev, dir, info, val);
  932. iio_device_release_direct(indio_dev);
  933. return ret ?: IIO_VAL_INT;
  934. }
  935. static int __ad4062_write_event_info_value(struct ad4062_state *st,
  936. enum iio_event_direction dir, int val)
  937. {
  938. u8 reg;
  939. if (val != sign_extend32(val, AD4062_LIMIT_BITS - 1))
  940. return -EINVAL;
  941. if (dir == IIO_EV_DIR_RISING)
  942. reg = AD4062_REG_MAX_LIMIT;
  943. else
  944. reg = AD4062_REG_MIN_LIMIT;
  945. st->buf.be16 = cpu_to_be16(val);
  946. return regmap_bulk_write(st->regmap, reg, &st->buf.be16,
  947. sizeof(st->buf.be16));
  948. }
  949. static int __ad4062_write_event_info_hysteresis(struct ad4062_state *st,
  950. enum iio_event_direction dir, int val)
  951. {
  952. u8 reg;
  953. if (val > BIT(7) - 1)
  954. return -EINVAL;
  955. if (dir == IIO_EV_DIR_RISING)
  956. reg = AD4062_REG_MAX_HYST;
  957. else
  958. reg = AD4062_REG_MIN_HYST;
  959. return regmap_write(st->regmap, reg, val);
  960. }
  961. static int ad4062_write_event_value_dispatch(struct iio_dev *indio_dev,
  962. enum iio_event_type type,
  963. enum iio_event_direction dir,
  964. enum iio_event_info info, int val)
  965. {
  966. struct ad4062_state *st = iio_priv(indio_dev);
  967. if (st->wait_event)
  968. return -EBUSY;
  969. switch (type) {
  970. case IIO_EV_TYPE_THRESH:
  971. switch (info) {
  972. case IIO_EV_INFO_VALUE:
  973. return __ad4062_write_event_info_value(st, dir, val);
  974. case IIO_EV_INFO_HYSTERESIS:
  975. return __ad4062_write_event_info_hysteresis(st, dir, val);
  976. default:
  977. return -EINVAL;
  978. }
  979. default:
  980. return -EINVAL;
  981. }
  982. }
  983. static int ad4062_write_event_value(struct iio_dev *indio_dev,
  984. const struct iio_chan_spec *chan,
  985. enum iio_event_type type,
  986. enum iio_event_direction dir,
  987. enum iio_event_info info, int val,
  988. int val2)
  989. {
  990. int ret;
  991. if (!iio_device_claim_direct(indio_dev))
  992. return -EBUSY;
  993. ret = ad4062_write_event_value_dispatch(indio_dev, type, dir, info, val);
  994. iio_device_release_direct(indio_dev);
  995. return ret;
  996. }
  997. /*
  998. * The AD4062 in burst averaging mode increases realbits from 16-bits to
  999. * 20-bits, increasing the storagebits from 16-bits to 32-bits.
  1000. */
  1001. static inline size_t ad4062_sizeof_storagebits(struct ad4062_state *st)
  1002. {
  1003. const struct iio_scan_type *scan_type =
  1004. iio_get_current_scan_type(st->indio_dev, st->chip->channels);
  1005. return BITS_TO_BYTES(scan_type->storagebits);
  1006. }
  1007. /* Read registers only with realbits (no sign extension bytes) */
  1008. static inline size_t ad4062_get_conv_addr(struct ad4062_state *st, size_t _sizeof)
  1009. {
  1010. if (st->gpo_irq[1])
  1011. return _sizeof == sizeof(u32) ? AD4062_REG_CONV_READ_32BITS :
  1012. AD4062_REG_CONV_READ_16BITS;
  1013. return _sizeof == sizeof(u32) ? AD4062_REG_CONV_TRIGGER_32BITS :
  1014. AD4062_REG_CONV_TRIGGER_16BITS;
  1015. }
  1016. static int pm_ad4062_triggered_buffer_postenable(struct ad4062_state *st)
  1017. {
  1018. int ret;
  1019. PM_RUNTIME_ACQUIRE(&st->i3cdev->dev, pm);
  1020. ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
  1021. if (ret)
  1022. return ret;
  1023. if (st->wait_event)
  1024. return -EBUSY;
  1025. ret = ad4062_set_operation_mode(st, st->mode);
  1026. if (ret)
  1027. return ret;
  1028. st->conv_sizeof = ad4062_sizeof_storagebits(st);
  1029. st->conv_addr = ad4062_get_conv_addr(st, st->conv_sizeof);
  1030. /* CONV_READ requires read to trigger first sample. */
  1031. struct i3c_xfer xfer_sample[2] = {
  1032. {
  1033. .data.out = &st->conv_addr,
  1034. .len = sizeof(st->conv_addr),
  1035. .rnw = false,
  1036. },
  1037. {
  1038. .data.in = &st->buf.be32,
  1039. .len = sizeof(st->buf.be32),
  1040. .rnw = true,
  1041. }
  1042. };
  1043. return i3c_device_do_xfers(st->i3cdev, xfer_sample,
  1044. st->gpo_irq[1] ? 2 : 1, I3C_SDR);
  1045. }
  1046. static int ad4062_triggered_buffer_postenable(struct iio_dev *indio_dev)
  1047. {
  1048. struct ad4062_state *st = iio_priv(indio_dev);
  1049. int ret;
  1050. ret = pm_ad4062_triggered_buffer_postenable(st);
  1051. if (ret)
  1052. return ret;
  1053. pm_runtime_get_noresume(&st->i3cdev->dev);
  1054. return 0;
  1055. }
  1056. static int ad4062_triggered_buffer_predisable(struct iio_dev *indio_dev)
  1057. {
  1058. struct ad4062_state *st = iio_priv(indio_dev);
  1059. pm_runtime_put_autosuspend(&st->i3cdev->dev);
  1060. return 0;
  1061. }
  1062. static const struct iio_buffer_setup_ops ad4062_triggered_buffer_setup_ops = {
  1063. .postenable = &ad4062_triggered_buffer_postenable,
  1064. .predisable = &ad4062_triggered_buffer_predisable,
  1065. };
  1066. static int ad4062_debugfs_reg_access(struct iio_dev *indio_dev, unsigned int reg,
  1067. unsigned int writeval, unsigned int *readval)
  1068. {
  1069. struct ad4062_state *st = iio_priv(indio_dev);
  1070. if (readval)
  1071. return regmap_read(st->regmap, reg, readval);
  1072. else
  1073. return regmap_write(st->regmap, reg, writeval);
  1074. }
  1075. static int ad4062_get_current_scan_type(const struct iio_dev *indio_dev,
  1076. const struct iio_chan_spec *chan)
  1077. {
  1078. struct ad4062_state *st = iio_priv(indio_dev);
  1079. return st->mode == AD4062_BURST_AVERAGING_MODE ?
  1080. AD4062_SCAN_TYPE_BURST_AVG :
  1081. AD4062_SCAN_TYPE_SAMPLE;
  1082. }
  1083. static const struct iio_info ad4062_info = {
  1084. .read_raw = ad4062_read_raw,
  1085. .write_raw = ad4062_write_raw,
  1086. .read_avail = ad4062_read_avail,
  1087. .read_event_config = ad4062_read_event_config,
  1088. .write_event_config = ad4062_write_event_config,
  1089. .read_event_value = ad4062_read_event_value,
  1090. .write_event_value = ad4062_write_event_value,
  1091. .event_attrs = &ad4062_event_attribute_group,
  1092. .get_current_scan_type = ad4062_get_current_scan_type,
  1093. .debugfs_reg_access = ad4062_debugfs_reg_access,
  1094. };
  1095. static const struct regmap_config ad4062_regmap_config = {
  1096. .name = "ad4062",
  1097. .reg_bits = 8,
  1098. .val_bits = 8,
  1099. .max_register = AD4062_MAX_REG,
  1100. .rd_table = &ad4062_regmap_rd_table,
  1101. .wr_table = &ad4062_regmap_wr_table,
  1102. .can_sleep = true,
  1103. };
  1104. static int ad4062_regulators_get(struct ad4062_state *st, bool *ref_sel)
  1105. {
  1106. struct device *dev = &st->i3cdev->dev;
  1107. int ret;
  1108. ret = devm_regulator_get_enable(dev, "vio");
  1109. if (ret)
  1110. return dev_err_probe(dev, ret, "Failed to enable vio voltage\n");
  1111. st->vref_uV = devm_regulator_get_enable_read_voltage(dev, "ref");
  1112. *ref_sel = st->vref_uV == -ENODEV;
  1113. if (st->vref_uV < 0 && !*ref_sel)
  1114. return dev_err_probe(dev, st->vref_uV,
  1115. "Failed to enable and read ref voltage\n");
  1116. if (*ref_sel) {
  1117. st->vref_uV = devm_regulator_get_enable_read_voltage(dev, "vdd");
  1118. if (st->vref_uV < 0)
  1119. return dev_err_probe(dev, st->vref_uV,
  1120. "Failed to enable and read vdd voltage\n");
  1121. } else {
  1122. ret = devm_regulator_get_enable(dev, "vdd");
  1123. if (ret)
  1124. return dev_err_probe(dev, ret,
  1125. "Failed to enable vdd regulator\n");
  1126. }
  1127. return 0;
  1128. }
  1129. static int ad4062_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  1130. {
  1131. return GPIO_LINE_DIRECTION_OUT;
  1132. }
  1133. static int ad4062_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
  1134. {
  1135. struct ad4062_state *st = gpiochip_get_data(gc);
  1136. unsigned int reg_val = value ? AD4062_GP_STATIC_HIGH : AD4062_GP_STATIC_LOW;
  1137. if (offset)
  1138. return regmap_update_bits(st->regmap, AD4062_REG_GP_CONF,
  1139. AD4062_REG_GP_CONF_MODE_MSK_1,
  1140. FIELD_PREP(AD4062_REG_GP_CONF_MODE_MSK_1, reg_val));
  1141. else
  1142. return regmap_update_bits(st->regmap, AD4062_REG_GP_CONF,
  1143. AD4062_REG_GP_CONF_MODE_MSK_0,
  1144. FIELD_PREP(AD4062_REG_GP_CONF_MODE_MSK_0, reg_val));
  1145. }
  1146. static int ad4062_gpio_get(struct gpio_chip *gc, unsigned int offset)
  1147. {
  1148. struct ad4062_state *st = gpiochip_get_data(gc);
  1149. unsigned int reg_val;
  1150. int ret;
  1151. ret = regmap_read(st->regmap, AD4062_REG_GP_CONF, &reg_val);
  1152. if (ret)
  1153. return ret;
  1154. if (offset)
  1155. reg_val = FIELD_GET(AD4062_REG_GP_CONF_MODE_MSK_1, reg_val);
  1156. else
  1157. reg_val = FIELD_GET(AD4062_REG_GP_CONF_MODE_MSK_0, reg_val);
  1158. return reg_val == AD4062_GP_STATIC_HIGH;
  1159. }
  1160. static void ad4062_gpio_disable(void *data)
  1161. {
  1162. struct ad4062_state *st = data;
  1163. u8 val = FIELD_PREP(AD4062_REG_GP_CONF_MODE_MSK_0, AD4062_GP_DISABLED) |
  1164. FIELD_PREP(AD4062_REG_GP_CONF_MODE_MSK_1, AD4062_GP_DISABLED);
  1165. regmap_update_bits(st->regmap, AD4062_REG_GP_CONF,
  1166. AD4062_REG_GP_CONF_MODE_MSK_1 | AD4062_REG_GP_CONF_MODE_MSK_0,
  1167. val);
  1168. }
  1169. static int ad4062_gpio_init_valid_mask(struct gpio_chip *gc,
  1170. unsigned long *valid_mask,
  1171. unsigned int ngpios)
  1172. {
  1173. struct ad4062_state *st = gpiochip_get_data(gc);
  1174. bitmap_zero(valid_mask, ngpios);
  1175. for (unsigned int i = 0; i < ARRAY_SIZE(st->gpo_irq); i++)
  1176. __assign_bit(i, valid_mask, !st->gpo_irq[i]);
  1177. return 0;
  1178. }
  1179. static int ad4062_gpio_init(struct ad4062_state *st)
  1180. {
  1181. struct device *dev = &st->i3cdev->dev;
  1182. struct gpio_chip *gc;
  1183. u8 val, mask;
  1184. int ret;
  1185. if (!device_property_read_bool(dev, "gpio-controller"))
  1186. return 0;
  1187. gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
  1188. if (!gc)
  1189. return -ENOMEM;
  1190. val = 0;
  1191. mask = 0;
  1192. if (!st->gpo_irq[0]) {
  1193. mask |= AD4062_REG_GP_CONF_MODE_MSK_0;
  1194. val |= FIELD_PREP(AD4062_REG_GP_CONF_MODE_MSK_0, AD4062_GP_STATIC_LOW);
  1195. }
  1196. if (!st->gpo_irq[1]) {
  1197. mask |= AD4062_REG_GP_CONF_MODE_MSK_1;
  1198. val |= FIELD_PREP(AD4062_REG_GP_CONF_MODE_MSK_1, AD4062_GP_STATIC_LOW);
  1199. }
  1200. ret = regmap_update_bits(st->regmap, AD4062_REG_GP_CONF,
  1201. mask, val);
  1202. if (ret)
  1203. return ret;
  1204. ret = devm_add_action_or_reset(dev, ad4062_gpio_disable, st);
  1205. if (ret)
  1206. return ret;
  1207. gc->parent = dev;
  1208. gc->label = st->chip->name;
  1209. gc->owner = THIS_MODULE;
  1210. gc->base = -1;
  1211. gc->ngpio = 2;
  1212. gc->init_valid_mask = ad4062_gpio_init_valid_mask;
  1213. gc->get_direction = ad4062_gpio_get_direction;
  1214. gc->set = ad4062_gpio_set;
  1215. gc->get = ad4062_gpio_get;
  1216. gc->can_sleep = true;
  1217. ret = devm_gpiochip_add_data(dev, gc, st);
  1218. if (ret)
  1219. return dev_err_probe(dev, ret, "Unable to register GPIO chip\n");
  1220. return 0;
  1221. }
  1222. static const struct i3c_device_id ad4062_id_table[] = {
  1223. I3C_DEVICE(AD4062_I3C_VENDOR, AD4060_PROD_ID, &ad4060_chip_info),
  1224. I3C_DEVICE(AD4062_I3C_VENDOR, AD4062_PROD_ID, &ad4062_chip_info),
  1225. { }
  1226. };
  1227. MODULE_DEVICE_TABLE(i3c, ad4062_id_table);
  1228. static int ad4062_probe(struct i3c_device *i3cdev)
  1229. {
  1230. const struct i3c_device_id *id = i3c_device_match_id(i3cdev, ad4062_id_table);
  1231. const struct ad4062_chip_info *chip = id->data;
  1232. struct device *dev = &i3cdev->dev;
  1233. struct iio_dev *indio_dev;
  1234. struct ad4062_state *st;
  1235. bool ref_sel;
  1236. int ret;
  1237. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  1238. if (!indio_dev)
  1239. return -ENOMEM;
  1240. st = iio_priv(indio_dev);
  1241. st->i3cdev = i3cdev;
  1242. i3cdev_set_drvdata(i3cdev, st);
  1243. init_completion(&st->completion);
  1244. ret = ad4062_regulators_get(st, &ref_sel);
  1245. if (ret)
  1246. return ret;
  1247. st->regmap = devm_regmap_init_i3c(i3cdev, &ad4062_regmap_config);
  1248. if (IS_ERR(st->regmap))
  1249. return dev_err_probe(dev, PTR_ERR(st->regmap),
  1250. "Failed to initialize regmap\n");
  1251. st->mode = AD4062_SAMPLE_MODE;
  1252. st->wait_event = false;
  1253. st->chip = chip;
  1254. st->sampling_frequency = 0;
  1255. st->events_frequency = 0;
  1256. st->oversamp_ratio = 0;
  1257. st->indio_dev = indio_dev;
  1258. indio_dev->modes = INDIO_DIRECT_MODE;
  1259. indio_dev->num_channels = 1;
  1260. indio_dev->info = &ad4062_info;
  1261. indio_dev->name = chip->name;
  1262. indio_dev->channels = chip->channels;
  1263. ret = ad4062_soft_reset(st);
  1264. if (ret)
  1265. return dev_err_probe(dev, ret, "AD4062 failed to soft reset\n");
  1266. ret = ad4062_check_ids(st);
  1267. if (ret)
  1268. return ret;
  1269. ret = ad4062_setup(indio_dev, indio_dev->channels, &ref_sel);
  1270. if (ret)
  1271. return ret;
  1272. ret = ad4062_request_irq(indio_dev);
  1273. if (ret)
  1274. return ret;
  1275. ret = ad4062_request_trigger(indio_dev);
  1276. if (ret)
  1277. return ret;
  1278. ret = devm_iio_triggered_buffer_setup(&i3cdev->dev, indio_dev,
  1279. iio_pollfunc_store_time,
  1280. ad4062_poll_handler,
  1281. &ad4062_triggered_buffer_setup_ops);
  1282. if (ret)
  1283. return ret;
  1284. pm_runtime_set_active(dev);
  1285. ret = devm_pm_runtime_enable(dev);
  1286. if (ret)
  1287. return dev_err_probe(dev, ret, "Failed to enable pm_runtime\n");
  1288. pm_runtime_set_autosuspend_delay(dev, 1000);
  1289. pm_runtime_use_autosuspend(dev);
  1290. ret = ad4062_request_ibi(i3cdev);
  1291. if (ret)
  1292. return dev_err_probe(dev, ret, "Failed to request i3c ibi\n");
  1293. ret = ad4062_gpio_init(st);
  1294. if (ret)
  1295. return ret;
  1296. ret = devm_work_autocancel(dev, &st->trig_conv, ad4062_trigger_work);
  1297. if (ret)
  1298. return ret;
  1299. return devm_iio_device_register(dev, indio_dev);
  1300. }
  1301. static int ad4062_runtime_suspend(struct device *dev)
  1302. {
  1303. struct ad4062_state *st = dev_get_drvdata(dev);
  1304. return regmap_write(st->regmap, AD4062_REG_DEVICE_CONFIG,
  1305. FIELD_PREP(AD4062_REG_DEVICE_CONFIG_POWER_MODE_MSK,
  1306. AD4062_REG_DEVICE_CONFIG_LOW_POWER_MODE));
  1307. }
  1308. static int ad4062_runtime_resume(struct device *dev)
  1309. {
  1310. struct ad4062_state *st = dev_get_drvdata(dev);
  1311. int ret;
  1312. ret = regmap_clear_bits(st->regmap, AD4062_REG_DEVICE_CONFIG,
  1313. AD4062_REG_DEVICE_CONFIG_POWER_MODE_MSK);
  1314. if (ret)
  1315. return ret;
  1316. /* Wait device functional blocks to power up */
  1317. fsleep(3 * USEC_PER_MSEC);
  1318. return 0;
  1319. }
  1320. static DEFINE_RUNTIME_DEV_PM_OPS(ad4062_pm_ops,
  1321. ad4062_runtime_suspend, ad4062_runtime_resume, NULL);
  1322. static struct i3c_driver ad4062_driver = {
  1323. .driver = {
  1324. .name = "ad4062",
  1325. .pm = pm_ptr(&ad4062_pm_ops),
  1326. },
  1327. .probe = ad4062_probe,
  1328. .id_table = ad4062_id_table,
  1329. };
  1330. module_i3c_driver(ad4062_driver);
  1331. MODULE_AUTHOR("Jorge Marques <jorge.marques@analog.com>");
  1332. MODULE_DESCRIPTION("Analog Devices AD4062");
  1333. MODULE_LICENSE("GPL");