iio-rescale.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IIO rescale driver
  4. *
  5. * Copyright (C) 2018 Axentia Technologies AB
  6. * Copyright (C) 2022 Liam Beguin <[email protected]>
  7. *
  8. * Author: Peter Rosin <[email protected]>
  9. */
  10. #include <linux/err.h>
  11. #include <linux/gcd.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/property.h>
  16. #include <linux/iio/afe/rescale.h>
  17. #include <linux/iio/consumer.h>
  18. #include <linux/iio/iio.h>
  19. int rescale_process_scale(struct rescale *rescale, int scale_type,
  20. int *val, int *val2)
  21. {
  22. s64 tmp;
  23. int _val, _val2;
  24. s32 rem, rem2;
  25. u32 mult;
  26. u32 neg;
  27. switch (scale_type) {
  28. case IIO_VAL_INT:
  29. *val *= rescale->numerator;
  30. if (rescale->denominator == 1)
  31. return scale_type;
  32. *val2 = rescale->denominator;
  33. return IIO_VAL_FRACTIONAL;
  34. case IIO_VAL_FRACTIONAL:
  35. /*
  36. * When the product of both scales doesn't overflow, avoid
  37. * potential accuracy loss (for in kernel consumers) by
  38. * keeping a fractional representation.
  39. */
  40. if (!check_mul_overflow(*val, rescale->numerator, &_val) &&
  41. !check_mul_overflow(*val2, rescale->denominator, &_val2)) {
  42. *val = _val;
  43. *val2 = _val2;
  44. return IIO_VAL_FRACTIONAL;
  45. }
  46. fallthrough;
  47. case IIO_VAL_FRACTIONAL_LOG2:
  48. tmp = (s64)*val * 1000000000LL;
  49. tmp = div_s64(tmp, rescale->denominator);
  50. tmp *= rescale->numerator;
  51. tmp = div_s64_rem(tmp, 1000000000LL, &rem);
  52. *val = tmp;
  53. if (!rem)
  54. return scale_type;
  55. if (scale_type == IIO_VAL_FRACTIONAL)
  56. tmp = *val2;
  57. else
  58. tmp = ULL(1) << *val2;
  59. rem2 = *val % (int)tmp;
  60. *val = *val / (int)tmp;
  61. *val2 = rem / (int)tmp;
  62. if (rem2)
  63. *val2 += div_s64((s64)rem2 * 1000000000LL, tmp);
  64. return IIO_VAL_INT_PLUS_NANO;
  65. case IIO_VAL_INT_PLUS_NANO:
  66. case IIO_VAL_INT_PLUS_MICRO:
  67. mult = scale_type == IIO_VAL_INT_PLUS_NANO ? 1000000000L : 1000000L;
  68. /*
  69. * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if either *val
  70. * OR *val2 is negative the schan scale is negative, i.e.
  71. * *val = 1 and *val2 = -0.5 yields -1.5 not -0.5.
  72. */
  73. neg = *val < 0 || *val2 < 0;
  74. tmp = (s64)abs(*val) * abs(rescale->numerator);
  75. *val = div_s64_rem(tmp, abs(rescale->denominator), &rem);
  76. tmp = (s64)rem * mult + (s64)abs(*val2) * abs(rescale->numerator);
  77. tmp = div_s64(tmp, abs(rescale->denominator));
  78. *val += div_s64_rem(tmp, mult, val2);
  79. /*
  80. * If only one of the rescaler elements or the schan scale is
  81. * negative, the combined scale is negative.
  82. */
  83. if (neg ^ ((rescale->numerator < 0) ^ (rescale->denominator < 0))) {
  84. if (*val)
  85. *val = -*val;
  86. else
  87. *val2 = -*val2;
  88. }
  89. return scale_type;
  90. default:
  91. return -EOPNOTSUPP;
  92. }
  93. }
  94. EXPORT_SYMBOL_NS_GPL(rescale_process_scale, IIO_RESCALE);
  95. int rescale_process_offset(struct rescale *rescale, int scale_type,
  96. int scale, int scale2, int schan_off,
  97. int *val, int *val2)
  98. {
  99. s64 tmp, tmp2;
  100. switch (scale_type) {
  101. case IIO_VAL_FRACTIONAL:
  102. tmp = (s64)rescale->offset * scale2;
  103. *val = div_s64(tmp, scale) + schan_off;
  104. return IIO_VAL_INT;
  105. case IIO_VAL_INT:
  106. *val = div_s64(rescale->offset, scale) + schan_off;
  107. return IIO_VAL_INT;
  108. case IIO_VAL_FRACTIONAL_LOG2:
  109. tmp = (s64)rescale->offset * (1 << scale2);
  110. *val = div_s64(tmp, scale) + schan_off;
  111. return IIO_VAL_INT;
  112. case IIO_VAL_INT_PLUS_NANO:
  113. tmp = (s64)rescale->offset * 1000000000LL;
  114. tmp2 = ((s64)scale * 1000000000LL) + scale2;
  115. *val = div64_s64(tmp, tmp2) + schan_off;
  116. return IIO_VAL_INT;
  117. case IIO_VAL_INT_PLUS_MICRO:
  118. tmp = (s64)rescale->offset * 1000000LL;
  119. tmp2 = ((s64)scale * 1000000LL) + scale2;
  120. *val = div64_s64(tmp, tmp2) + schan_off;
  121. return IIO_VAL_INT;
  122. default:
  123. return -EOPNOTSUPP;
  124. }
  125. }
  126. EXPORT_SYMBOL_NS_GPL(rescale_process_offset, IIO_RESCALE);
  127. static int rescale_read_raw(struct iio_dev *indio_dev,
  128. struct iio_chan_spec const *chan,
  129. int *val, int *val2, long mask)
  130. {
  131. struct rescale *rescale = iio_priv(indio_dev);
  132. int scale, scale2;
  133. int schan_off = 0;
  134. int ret;
  135. switch (mask) {
  136. case IIO_CHAN_INFO_RAW:
  137. if (rescale->chan_processed)
  138. /*
  139. * When only processed channels are supported, we
  140. * read the processed data and scale it by 1/1
  141. * augmented with whatever the rescaler has calculated.
  142. */
  143. return iio_read_channel_processed(rescale->source, val);
  144. else
  145. return iio_read_channel_raw(rescale->source, val);
  146. case IIO_CHAN_INFO_SCALE:
  147. if (rescale->chan_processed) {
  148. /*
  149. * Processed channels are scaled 1-to-1
  150. */
  151. *val = 1;
  152. *val2 = 1;
  153. ret = IIO_VAL_FRACTIONAL;
  154. } else {
  155. ret = iio_read_channel_scale(rescale->source, val, val2);
  156. }
  157. return rescale_process_scale(rescale, ret, val, val2);
  158. case IIO_CHAN_INFO_OFFSET:
  159. /*
  160. * Processed channels are scaled 1-to-1 and source offset is
  161. * already taken into account.
  162. *
  163. * In other cases, real world measurement are expressed as:
  164. *
  165. * schan_scale * (raw + schan_offset)
  166. *
  167. * Given that the rescaler parameters are applied recursively:
  168. *
  169. * rescaler_scale * (schan_scale * (raw + schan_offset) +
  170. * rescaler_offset)
  171. *
  172. * Or,
  173. *
  174. * (rescaler_scale * schan_scale) * (raw +
  175. * (schan_offset + rescaler_offset / schan_scale)
  176. *
  177. * Thus, reusing the original expression the parameters exposed
  178. * to userspace are:
  179. *
  180. * scale = schan_scale * rescaler_scale
  181. * offset = schan_offset + rescaler_offset / schan_scale
  182. */
  183. if (rescale->chan_processed) {
  184. *val = rescale->offset;
  185. return IIO_VAL_INT;
  186. }
  187. if (iio_channel_has_info(rescale->source->channel,
  188. IIO_CHAN_INFO_OFFSET)) {
  189. ret = iio_read_channel_offset(rescale->source,
  190. &schan_off, NULL);
  191. if (ret != IIO_VAL_INT)
  192. return ret < 0 ? ret : -EOPNOTSUPP;
  193. }
  194. if (iio_channel_has_info(rescale->source->channel,
  195. IIO_CHAN_INFO_SCALE)) {
  196. ret = iio_read_channel_scale(rescale->source, &scale, &scale2);
  197. return rescale_process_offset(rescale, ret, scale, scale2,
  198. schan_off, val, val2);
  199. }
  200. /*
  201. * If we get here we have no scale so scale 1:1 but apply
  202. * rescaler and offset, if any.
  203. */
  204. return rescale_process_offset(rescale, IIO_VAL_FRACTIONAL, 1, 1,
  205. schan_off, val, val2);
  206. default:
  207. return -EINVAL;
  208. }
  209. }
  210. static int rescale_read_avail(struct iio_dev *indio_dev,
  211. struct iio_chan_spec const *chan,
  212. const int **vals, int *type, int *length,
  213. long mask)
  214. {
  215. struct rescale *rescale = iio_priv(indio_dev);
  216. switch (mask) {
  217. case IIO_CHAN_INFO_RAW:
  218. *type = IIO_VAL_INT;
  219. return iio_read_avail_channel_raw(rescale->source,
  220. vals, length);
  221. default:
  222. return -EINVAL;
  223. }
  224. }
  225. static const struct iio_info rescale_info = {
  226. .read_raw = rescale_read_raw,
  227. .read_avail = rescale_read_avail,
  228. };
  229. static ssize_t rescale_read_ext_info(struct iio_dev *indio_dev,
  230. uintptr_t private,
  231. struct iio_chan_spec const *chan,
  232. char *buf)
  233. {
  234. struct rescale *rescale = iio_priv(indio_dev);
  235. return iio_read_channel_ext_info(rescale->source,
  236. rescale->ext_info[private].name,
  237. buf);
  238. }
  239. static ssize_t rescale_write_ext_info(struct iio_dev *indio_dev,
  240. uintptr_t private,
  241. struct iio_chan_spec const *chan,
  242. const char *buf, size_t len)
  243. {
  244. struct rescale *rescale = iio_priv(indio_dev);
  245. return iio_write_channel_ext_info(rescale->source,
  246. rescale->ext_info[private].name,
  247. buf, len);
  248. }
  249. static int rescale_configure_channel(struct device *dev,
  250. struct rescale *rescale)
  251. {
  252. struct iio_chan_spec *chan = &rescale->chan;
  253. struct iio_chan_spec const *schan = rescale->source->channel;
  254. chan->indexed = 1;
  255. chan->output = schan->output;
  256. chan->ext_info = rescale->ext_info;
  257. chan->type = rescale->cfg->type;
  258. if (iio_channel_has_info(schan, IIO_CHAN_INFO_RAW) &&
  259. (iio_channel_has_info(schan, IIO_CHAN_INFO_SCALE) ||
  260. iio_channel_has_info(schan, IIO_CHAN_INFO_OFFSET))) {
  261. dev_info(dev, "using raw+scale/offset source channel\n");
  262. } else if (iio_channel_has_info(schan, IIO_CHAN_INFO_PROCESSED)) {
  263. dev_info(dev, "using processed channel\n");
  264. rescale->chan_processed = true;
  265. } else {
  266. dev_err(dev, "source channel is not supported\n");
  267. return -EINVAL;
  268. }
  269. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  270. BIT(IIO_CHAN_INFO_SCALE);
  271. if (rescale->offset)
  272. chan->info_mask_separate |= BIT(IIO_CHAN_INFO_OFFSET);
  273. /*
  274. * Using .read_avail() is fringe to begin with and makes no sense
  275. * whatsoever for processed channels, so we make sure that this cannot
  276. * be called on a processed channel.
  277. */
  278. if (iio_channel_has_available(schan, IIO_CHAN_INFO_RAW) &&
  279. !rescale->chan_processed)
  280. chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_RAW);
  281. return 0;
  282. }
  283. static int rescale_current_sense_amplifier_props(struct device *dev,
  284. struct rescale *rescale)
  285. {
  286. u32 sense;
  287. u32 gain_mult = 1;
  288. u32 gain_div = 1;
  289. u32 factor;
  290. int ret;
  291. ret = device_property_read_u32(dev, "sense-resistor-micro-ohms",
  292. &sense);
  293. if (ret) {
  294. dev_err(dev, "failed to read the sense resistance: %d\n", ret);
  295. return ret;
  296. }
  297. device_property_read_u32(dev, "sense-gain-mult", &gain_mult);
  298. device_property_read_u32(dev, "sense-gain-div", &gain_div);
  299. /*
  300. * Calculate the scaling factor, 1 / (gain * sense), or
  301. * gain_div / (gain_mult * sense), while trying to keep the
  302. * numerator/denominator from overflowing.
  303. */
  304. factor = gcd(sense, 1000000);
  305. rescale->numerator = 1000000 / factor;
  306. rescale->denominator = sense / factor;
  307. factor = gcd(rescale->numerator, gain_mult);
  308. rescale->numerator /= factor;
  309. rescale->denominator *= gain_mult / factor;
  310. factor = gcd(rescale->denominator, gain_div);
  311. rescale->numerator *= gain_div / factor;
  312. rescale->denominator /= factor;
  313. return 0;
  314. }
  315. static int rescale_current_sense_shunt_props(struct device *dev,
  316. struct rescale *rescale)
  317. {
  318. u32 shunt;
  319. u32 factor;
  320. int ret;
  321. ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms",
  322. &shunt);
  323. if (ret) {
  324. dev_err(dev, "failed to read the shunt resistance: %d\n", ret);
  325. return ret;
  326. }
  327. factor = gcd(shunt, 1000000);
  328. rescale->numerator = 1000000 / factor;
  329. rescale->denominator = shunt / factor;
  330. return 0;
  331. }
  332. static int rescale_voltage_divider_props(struct device *dev,
  333. struct rescale *rescale)
  334. {
  335. int ret;
  336. u32 factor;
  337. ret = device_property_read_u32(dev, "output-ohms",
  338. &rescale->denominator);
  339. if (ret) {
  340. dev_err(dev, "failed to read output-ohms: %d\n", ret);
  341. return ret;
  342. }
  343. ret = device_property_read_u32(dev, "full-ohms",
  344. &rescale->numerator);
  345. if (ret) {
  346. dev_err(dev, "failed to read full-ohms: %d\n", ret);
  347. return ret;
  348. }
  349. factor = gcd(rescale->numerator, rescale->denominator);
  350. rescale->numerator /= factor;
  351. rescale->denominator /= factor;
  352. return 0;
  353. }
  354. static int rescale_temp_sense_rtd_props(struct device *dev,
  355. struct rescale *rescale)
  356. {
  357. u32 factor;
  358. u32 alpha;
  359. u32 iexc;
  360. u32 tmp;
  361. int ret;
  362. u32 r0;
  363. ret = device_property_read_u32(dev, "excitation-current-microamp",
  364. &iexc);
  365. if (ret) {
  366. dev_err(dev, "failed to read excitation-current-microamp: %d\n",
  367. ret);
  368. return ret;
  369. }
  370. ret = device_property_read_u32(dev, "alpha-ppm-per-celsius", &alpha);
  371. if (ret) {
  372. dev_err(dev, "failed to read alpha-ppm-per-celsius: %d\n",
  373. ret);
  374. return ret;
  375. }
  376. ret = device_property_read_u32(dev, "r-naught-ohms", &r0);
  377. if (ret) {
  378. dev_err(dev, "failed to read r-naught-ohms: %d\n", ret);
  379. return ret;
  380. }
  381. tmp = r0 * iexc * alpha / 1000000;
  382. factor = gcd(tmp, 1000000);
  383. rescale->numerator = 1000000 / factor;
  384. rescale->denominator = tmp / factor;
  385. rescale->offset = -1 * ((r0 * iexc) / 1000);
  386. return 0;
  387. }
  388. static int rescale_temp_transducer_props(struct device *dev,
  389. struct rescale *rescale)
  390. {
  391. s32 offset = 0;
  392. s32 sense = 1;
  393. s32 alpha;
  394. int ret;
  395. device_property_read_u32(dev, "sense-offset-millicelsius", &offset);
  396. device_property_read_u32(dev, "sense-resistor-ohms", &sense);
  397. ret = device_property_read_u32(dev, "alpha-ppm-per-celsius", &alpha);
  398. if (ret) {
  399. dev_err(dev, "failed to read alpha-ppm-per-celsius: %d\n", ret);
  400. return ret;
  401. }
  402. rescale->numerator = 1000000;
  403. rescale->denominator = alpha * sense;
  404. rescale->offset = div_s64((s64)offset * rescale->denominator,
  405. rescale->numerator);
  406. return 0;
  407. }
  408. enum rescale_variant {
  409. CURRENT_SENSE_AMPLIFIER,
  410. CURRENT_SENSE_SHUNT,
  411. VOLTAGE_DIVIDER,
  412. TEMP_SENSE_RTD,
  413. TEMP_TRANSDUCER,
  414. };
  415. static const struct rescale_cfg rescale_cfg[] = {
  416. [CURRENT_SENSE_AMPLIFIER] = {
  417. .type = IIO_CURRENT,
  418. .props = rescale_current_sense_amplifier_props,
  419. },
  420. [CURRENT_SENSE_SHUNT] = {
  421. .type = IIO_CURRENT,
  422. .props = rescale_current_sense_shunt_props,
  423. },
  424. [VOLTAGE_DIVIDER] = {
  425. .type = IIO_VOLTAGE,
  426. .props = rescale_voltage_divider_props,
  427. },
  428. [TEMP_SENSE_RTD] = {
  429. .type = IIO_TEMP,
  430. .props = rescale_temp_sense_rtd_props,
  431. },
  432. [TEMP_TRANSDUCER] = {
  433. .type = IIO_TEMP,
  434. .props = rescale_temp_transducer_props,
  435. },
  436. };
  437. static const struct of_device_id rescale_match[] = {
  438. { .compatible = "current-sense-amplifier",
  439. .data = &rescale_cfg[CURRENT_SENSE_AMPLIFIER], },
  440. { .compatible = "current-sense-shunt",
  441. .data = &rescale_cfg[CURRENT_SENSE_SHUNT], },
  442. { .compatible = "voltage-divider",
  443. .data = &rescale_cfg[VOLTAGE_DIVIDER], },
  444. { .compatible = "temperature-sense-rtd",
  445. .data = &rescale_cfg[TEMP_SENSE_RTD], },
  446. { .compatible = "temperature-transducer",
  447. .data = &rescale_cfg[TEMP_TRANSDUCER], },
  448. { /* sentinel */ }
  449. };
  450. MODULE_DEVICE_TABLE(of, rescale_match);
  451. static int rescale_probe(struct platform_device *pdev)
  452. {
  453. struct device *dev = &pdev->dev;
  454. struct iio_dev *indio_dev;
  455. struct iio_channel *source;
  456. struct rescale *rescale;
  457. int sizeof_ext_info;
  458. int sizeof_priv;
  459. int i;
  460. int ret;
  461. source = devm_iio_channel_get(dev, NULL);
  462. if (IS_ERR(source))
  463. return dev_err_probe(dev, PTR_ERR(source),
  464. "failed to get source channel\n");
  465. sizeof_ext_info = iio_get_channel_ext_info_count(source);
  466. if (sizeof_ext_info) {
  467. sizeof_ext_info += 1; /* one extra entry for the sentinel */
  468. sizeof_ext_info *= sizeof(*rescale->ext_info);
  469. }
  470. sizeof_priv = sizeof(*rescale) + sizeof_ext_info;
  471. indio_dev = devm_iio_device_alloc(dev, sizeof_priv);
  472. if (!indio_dev)
  473. return -ENOMEM;
  474. rescale = iio_priv(indio_dev);
  475. rescale->cfg = device_get_match_data(dev);
  476. rescale->numerator = 1;
  477. rescale->denominator = 1;
  478. rescale->offset = 0;
  479. ret = rescale->cfg->props(dev, rescale);
  480. if (ret)
  481. return ret;
  482. if (!rescale->numerator || !rescale->denominator) {
  483. dev_err(dev, "invalid scaling factor.\n");
  484. return -EINVAL;
  485. }
  486. platform_set_drvdata(pdev, indio_dev);
  487. rescale->source = source;
  488. indio_dev->name = dev_name(dev);
  489. indio_dev->info = &rescale_info;
  490. indio_dev->modes = INDIO_DIRECT_MODE;
  491. indio_dev->channels = &rescale->chan;
  492. indio_dev->num_channels = 1;
  493. if (sizeof_ext_info) {
  494. rescale->ext_info = devm_kmemdup(dev,
  495. source->channel->ext_info,
  496. sizeof_ext_info, GFP_KERNEL);
  497. if (!rescale->ext_info)
  498. return -ENOMEM;
  499. for (i = 0; rescale->ext_info[i].name; ++i) {
  500. struct iio_chan_spec_ext_info *ext_info =
  501. &rescale->ext_info[i];
  502. if (source->channel->ext_info[i].read)
  503. ext_info->read = rescale_read_ext_info;
  504. if (source->channel->ext_info[i].write)
  505. ext_info->write = rescale_write_ext_info;
  506. ext_info->private = i;
  507. }
  508. }
  509. ret = rescale_configure_channel(dev, rescale);
  510. if (ret)
  511. return ret;
  512. return devm_iio_device_register(dev, indio_dev);
  513. }
  514. static struct platform_driver rescale_driver = {
  515. .probe = rescale_probe,
  516. .driver = {
  517. .name = "iio-rescale",
  518. .of_match_table = rescale_match,
  519. },
  520. };
  521. module_platform_driver(rescale_driver);
  522. MODULE_DESCRIPTION("IIO rescale driver");
  523. MODULE_AUTHOR("Peter Rosin <[email protected]>");
  524. MODULE_LICENSE("GPL v2");