scmi_iio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface(SCMI) based IIO sensor driver
  4. *
  5. * Copyright (C) 2021 Google LLC
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/iio/buffer.h>
  10. #include <linux/iio/iio.h>
  11. #include <linux/iio/kfifo_buf.h>
  12. #include <linux/iio/sysfs.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kthread.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/scmi_protocol.h>
  18. #include <linux/time.h>
  19. #include <linux/types.h>
  20. #include <linux/units.h>
  21. #define SCMI_IIO_NUM_OF_AXIS 3
  22. struct scmi_iio_priv {
  23. const struct scmi_sensor_proto_ops *sensor_ops;
  24. struct scmi_protocol_handle *ph;
  25. const struct scmi_sensor_info *sensor_info;
  26. struct iio_dev *indio_dev;
  27. /* lock to protect against multiple access to the device */
  28. struct mutex lock;
  29. /* adding one additional channel for timestamp */
  30. s64 iio_buf[SCMI_IIO_NUM_OF_AXIS + 1];
  31. struct notifier_block sensor_update_nb;
  32. u32 *freq_avail;
  33. };
  34. static int scmi_iio_sensor_update_cb(struct notifier_block *nb,
  35. unsigned long event, void *data)
  36. {
  37. struct scmi_sensor_update_report *sensor_update = data;
  38. struct iio_dev *scmi_iio_dev;
  39. struct scmi_iio_priv *sensor;
  40. s8 tstamp_scale;
  41. u64 time, time_ns;
  42. int i;
  43. if (sensor_update->readings_count == 0)
  44. return NOTIFY_DONE;
  45. sensor = container_of(nb, struct scmi_iio_priv, sensor_update_nb);
  46. for (i = 0; i < sensor_update->readings_count; i++)
  47. sensor->iio_buf[i] = sensor_update->readings[i].value;
  48. if (!sensor->sensor_info->timestamped) {
  49. time_ns = ktime_to_ns(sensor_update->timestamp);
  50. } else {
  51. /*
  52. * All the axes are supposed to have the same value for timestamp.
  53. * We are just using the values from the Axis 0 here.
  54. */
  55. time = sensor_update->readings[0].timestamp;
  56. /*
  57. * Timestamp returned by SCMI is in seconds and is equal to
  58. * time * power-of-10 multiplier(tstamp_scale) seconds.
  59. * Converting the timestamp to nanoseconds below.
  60. */
  61. tstamp_scale = sensor->sensor_info->tstamp_scale +
  62. const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
  63. if (tstamp_scale < 0) {
  64. do_div(time, int_pow(10, abs(tstamp_scale)));
  65. time_ns = time;
  66. } else {
  67. time_ns = time * int_pow(10, tstamp_scale);
  68. }
  69. }
  70. scmi_iio_dev = sensor->indio_dev;
  71. iio_push_to_buffers_with_timestamp(scmi_iio_dev, sensor->iio_buf,
  72. time_ns);
  73. return NOTIFY_OK;
  74. }
  75. static int scmi_iio_buffer_preenable(struct iio_dev *iio_dev)
  76. {
  77. struct scmi_iio_priv *sensor = iio_priv(iio_dev);
  78. u32 sensor_config = 0;
  79. int err;
  80. if (sensor->sensor_info->timestamped)
  81. sensor_config |= FIELD_PREP(SCMI_SENS_CFG_TSTAMP_ENABLED_MASK,
  82. SCMI_SENS_CFG_TSTAMP_ENABLE);
  83. sensor_config |= FIELD_PREP(SCMI_SENS_CFG_SENSOR_ENABLED_MASK,
  84. SCMI_SENS_CFG_SENSOR_ENABLE);
  85. err = sensor->sensor_ops->config_set(sensor->ph,
  86. sensor->sensor_info->id,
  87. sensor_config);
  88. if (err)
  89. dev_err(&iio_dev->dev, "Error in enabling sensor %s err %d",
  90. sensor->sensor_info->name, err);
  91. return err;
  92. }
  93. static int scmi_iio_buffer_postdisable(struct iio_dev *iio_dev)
  94. {
  95. struct scmi_iio_priv *sensor = iio_priv(iio_dev);
  96. u32 sensor_config = 0;
  97. int err;
  98. sensor_config |= FIELD_PREP(SCMI_SENS_CFG_SENSOR_ENABLED_MASK,
  99. SCMI_SENS_CFG_SENSOR_DISABLE);
  100. err = sensor->sensor_ops->config_set(sensor->ph,
  101. sensor->sensor_info->id,
  102. sensor_config);
  103. if (err) {
  104. dev_err(&iio_dev->dev,
  105. "Error in disabling sensor %s with err %d",
  106. sensor->sensor_info->name, err);
  107. }
  108. return err;
  109. }
  110. static const struct iio_buffer_setup_ops scmi_iio_buffer_ops = {
  111. .preenable = scmi_iio_buffer_preenable,
  112. .postdisable = scmi_iio_buffer_postdisable,
  113. };
  114. static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
  115. {
  116. struct scmi_iio_priv *sensor = iio_priv(iio_dev);
  117. u64 sec, mult, uHz, sf;
  118. u32 sensor_config;
  119. char buf[32];
  120. int err = sensor->sensor_ops->config_get(sensor->ph,
  121. sensor->sensor_info->id,
  122. &sensor_config);
  123. if (err) {
  124. dev_err(&iio_dev->dev,
  125. "Error in getting sensor config for sensor %s err %d",
  126. sensor->sensor_info->name, err);
  127. return err;
  128. }
  129. uHz = val * MICROHZ_PER_HZ + val2;
  130. /*
  131. * The seconds field in the sensor interval in SCMI is 16 bits long
  132. * Therefore seconds = 1/Hz <= 0xFFFF. As floating point calculations are
  133. * discouraged in the kernel driver code, to calculate the scale factor (sf)
  134. * (1* 1000000 * sf)/uHz <= 0xFFFF. Therefore, sf <= (uHz * 0xFFFF)/1000000
  135. * To calculate the multiplier,we convert the sf into char string and
  136. * count the number of characters
  137. */
  138. sf = uHz * 0xFFFF;
  139. do_div(sf, MICROHZ_PER_HZ);
  140. mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
  141. sec = int_pow(10, mult) * MICROHZ_PER_HZ;
  142. do_div(sec, uHz);
  143. if (sec == 0) {
  144. dev_err(&iio_dev->dev,
  145. "Trying to set invalid sensor update value for sensor %s",
  146. sensor->sensor_info->name);
  147. return -EINVAL;
  148. }
  149. sensor_config &= ~SCMI_SENS_CFG_UPDATE_SECS_MASK;
  150. sensor_config |= FIELD_PREP(SCMI_SENS_CFG_UPDATE_SECS_MASK, sec);
  151. sensor_config &= ~SCMI_SENS_CFG_UPDATE_EXP_MASK;
  152. sensor_config |= FIELD_PREP(SCMI_SENS_CFG_UPDATE_EXP_MASK, -mult);
  153. if (sensor->sensor_info->timestamped) {
  154. sensor_config &= ~SCMI_SENS_CFG_TSTAMP_ENABLED_MASK;
  155. sensor_config |= FIELD_PREP(SCMI_SENS_CFG_TSTAMP_ENABLED_MASK,
  156. SCMI_SENS_CFG_TSTAMP_ENABLE);
  157. }
  158. sensor_config &= ~SCMI_SENS_CFG_ROUND_MASK;
  159. sensor_config |=
  160. FIELD_PREP(SCMI_SENS_CFG_ROUND_MASK, SCMI_SENS_CFG_ROUND_AUTO);
  161. err = sensor->sensor_ops->config_set(sensor->ph,
  162. sensor->sensor_info->id,
  163. sensor_config);
  164. if (err)
  165. dev_err(&iio_dev->dev,
  166. "Error in setting sensor update interval for sensor %s value %u err %d",
  167. sensor->sensor_info->name, sensor_config, err);
  168. return err;
  169. }
  170. static int scmi_iio_write_raw(struct iio_dev *iio_dev,
  171. struct iio_chan_spec const *chan, int val,
  172. int val2, long mask)
  173. {
  174. struct scmi_iio_priv *sensor = iio_priv(iio_dev);
  175. int err;
  176. switch (mask) {
  177. case IIO_CHAN_INFO_SAMP_FREQ:
  178. mutex_lock(&sensor->lock);
  179. err = scmi_iio_set_odr_val(iio_dev, val, val2);
  180. mutex_unlock(&sensor->lock);
  181. return err;
  182. default:
  183. return -EINVAL;
  184. }
  185. }
  186. static int scmi_iio_read_avail(struct iio_dev *iio_dev,
  187. struct iio_chan_spec const *chan,
  188. const int **vals, int *type, int *length,
  189. long mask)
  190. {
  191. struct scmi_iio_priv *sensor = iio_priv(iio_dev);
  192. switch (mask) {
  193. case IIO_CHAN_INFO_SAMP_FREQ:
  194. *vals = sensor->freq_avail;
  195. *type = IIO_VAL_INT_PLUS_MICRO;
  196. *length = sensor->sensor_info->intervals.count * 2;
  197. if (sensor->sensor_info->intervals.segmented)
  198. return IIO_AVAIL_RANGE;
  199. else
  200. return IIO_AVAIL_LIST;
  201. default:
  202. return -EINVAL;
  203. }
  204. }
  205. static void convert_ns_to_freq(u64 interval_ns, u64 *hz, u64 *uhz)
  206. {
  207. u64 rem, freq;
  208. freq = NSEC_PER_SEC;
  209. rem = do_div(freq, interval_ns);
  210. *hz = freq;
  211. *uhz = rem * 1000000UL;
  212. do_div(*uhz, interval_ns);
  213. }
  214. static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
  215. {
  216. u64 sensor_update_interval, sensor_interval_mult, hz, uhz;
  217. struct scmi_iio_priv *sensor = iio_priv(iio_dev);
  218. u32 sensor_config;
  219. int mult;
  220. int err = sensor->sensor_ops->config_get(sensor->ph,
  221. sensor->sensor_info->id,
  222. &sensor_config);
  223. if (err) {
  224. dev_err(&iio_dev->dev,
  225. "Error in getting sensor config for sensor %s err %d",
  226. sensor->sensor_info->name, err);
  227. return err;
  228. }
  229. sensor_update_interval =
  230. SCMI_SENS_CFG_GET_UPDATE_SECS(sensor_config) * NSEC_PER_SEC;
  231. mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
  232. if (mult < 0) {
  233. sensor_interval_mult = int_pow(10, abs(mult));
  234. do_div(sensor_update_interval, sensor_interval_mult);
  235. } else {
  236. sensor_interval_mult = int_pow(10, mult);
  237. sensor_update_interval =
  238. sensor_update_interval * sensor_interval_mult;
  239. }
  240. convert_ns_to_freq(sensor_update_interval, &hz, &uhz);
  241. *val = hz;
  242. *val2 = uhz;
  243. return 0;
  244. }
  245. static int scmi_iio_read_channel_data(struct iio_dev *iio_dev,
  246. struct iio_chan_spec const *ch, int *val, int *val2)
  247. {
  248. struct scmi_iio_priv *sensor = iio_priv(iio_dev);
  249. u32 sensor_config;
  250. struct scmi_sensor_reading readings[SCMI_IIO_NUM_OF_AXIS];
  251. int err;
  252. sensor_config = FIELD_PREP(SCMI_SENS_CFG_SENSOR_ENABLED_MASK,
  253. SCMI_SENS_CFG_SENSOR_ENABLE);
  254. err = sensor->sensor_ops->config_set(
  255. sensor->ph, sensor->sensor_info->id, sensor_config);
  256. if (err) {
  257. dev_err(&iio_dev->dev,
  258. "Error in enabling sensor %s err %d",
  259. sensor->sensor_info->name, err);
  260. return err;
  261. }
  262. err = sensor->sensor_ops->reading_get_timestamped(
  263. sensor->ph, sensor->sensor_info->id,
  264. sensor->sensor_info->num_axis, readings);
  265. if (err) {
  266. dev_err(&iio_dev->dev,
  267. "Error in reading raw attribute for sensor %s err %d",
  268. sensor->sensor_info->name, err);
  269. return err;
  270. }
  271. sensor_config = FIELD_PREP(SCMI_SENS_CFG_SENSOR_ENABLED_MASK,
  272. SCMI_SENS_CFG_SENSOR_DISABLE);
  273. err = sensor->sensor_ops->config_set(
  274. sensor->ph, sensor->sensor_info->id, sensor_config);
  275. if (err) {
  276. dev_err(&iio_dev->dev,
  277. "Error in disabling sensor %s err %d",
  278. sensor->sensor_info->name, err);
  279. return err;
  280. }
  281. *val = lower_32_bits(readings[ch->scan_index].value);
  282. *val2 = upper_32_bits(readings[ch->scan_index].value);
  283. return IIO_VAL_INT_64;
  284. }
  285. static int scmi_iio_read_raw(struct iio_dev *iio_dev,
  286. struct iio_chan_spec const *ch, int *val,
  287. int *val2, long mask)
  288. {
  289. struct scmi_iio_priv *sensor = iio_priv(iio_dev);
  290. s8 scale;
  291. int ret;
  292. switch (mask) {
  293. case IIO_CHAN_INFO_SCALE:
  294. scale = sensor->sensor_info->axis[ch->scan_index].scale;
  295. if (scale < 0) {
  296. *val = 1;
  297. *val2 = int_pow(10, abs(scale));
  298. return IIO_VAL_FRACTIONAL;
  299. }
  300. *val = int_pow(10, scale);
  301. return IIO_VAL_INT;
  302. case IIO_CHAN_INFO_SAMP_FREQ:
  303. ret = scmi_iio_get_odr_val(iio_dev, val, val2);
  304. return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
  305. case IIO_CHAN_INFO_RAW:
  306. ret = iio_device_claim_direct_mode(iio_dev);
  307. if (ret)
  308. return ret;
  309. ret = scmi_iio_read_channel_data(iio_dev, ch, val, val2);
  310. iio_device_release_direct_mode(iio_dev);
  311. return ret;
  312. default:
  313. return -EINVAL;
  314. }
  315. }
  316. static const struct iio_info scmi_iio_info = {
  317. .read_raw = scmi_iio_read_raw,
  318. .read_avail = scmi_iio_read_avail,
  319. .write_raw = scmi_iio_write_raw,
  320. };
  321. static ssize_t scmi_iio_get_raw_available(struct iio_dev *iio_dev,
  322. uintptr_t private,
  323. const struct iio_chan_spec *chan,
  324. char *buf)
  325. {
  326. struct scmi_iio_priv *sensor = iio_priv(iio_dev);
  327. u64 resolution, rem;
  328. s64 min_range, max_range;
  329. s8 exponent, scale;
  330. int len = 0;
  331. /*
  332. * All the axes are supposed to have the same value for range and resolution.
  333. * We are just using the values from the Axis 0 here.
  334. */
  335. if (sensor->sensor_info->axis[0].extended_attrs) {
  336. min_range = sensor->sensor_info->axis[0].attrs.min_range;
  337. max_range = sensor->sensor_info->axis[0].attrs.max_range;
  338. resolution = sensor->sensor_info->axis[0].resolution;
  339. exponent = sensor->sensor_info->axis[0].exponent;
  340. scale = sensor->sensor_info->axis[0].scale;
  341. /*
  342. * To provide the raw value for the resolution to the userspace,
  343. * need to divide the resolution exponent by the sensor scale
  344. */
  345. exponent = exponent - scale;
  346. if (exponent < 0) {
  347. rem = do_div(resolution,
  348. int_pow(10, abs(exponent))
  349. );
  350. len = sysfs_emit(buf,
  351. "[%lld %llu.%llu %lld]\n", min_range,
  352. resolution, rem, max_range);
  353. } else {
  354. resolution = resolution * int_pow(10, exponent);
  355. len = sysfs_emit(buf, "[%lld %llu %lld]\n",
  356. min_range, resolution, max_range);
  357. }
  358. }
  359. return len;
  360. }
  361. static const struct iio_chan_spec_ext_info scmi_iio_ext_info[] = {
  362. {
  363. .name = "raw_available",
  364. .read = scmi_iio_get_raw_available,
  365. .shared = IIO_SHARED_BY_TYPE,
  366. },
  367. {},
  368. };
  369. static void scmi_iio_set_timestamp_channel(struct iio_chan_spec *iio_chan,
  370. int scan_index)
  371. {
  372. iio_chan->type = IIO_TIMESTAMP;
  373. iio_chan->channel = -1;
  374. iio_chan->scan_index = scan_index;
  375. iio_chan->scan_type.sign = 'u';
  376. iio_chan->scan_type.realbits = 64;
  377. iio_chan->scan_type.storagebits = 64;
  378. }
  379. static void scmi_iio_set_data_channel(struct iio_chan_spec *iio_chan,
  380. enum iio_chan_type type,
  381. enum iio_modifier mod, int scan_index)
  382. {
  383. iio_chan->type = type;
  384. iio_chan->modified = 1;
  385. iio_chan->channel2 = mod;
  386. iio_chan->info_mask_separate =
  387. BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_RAW);
  388. iio_chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ);
  389. iio_chan->info_mask_shared_by_type_available =
  390. BIT(IIO_CHAN_INFO_SAMP_FREQ);
  391. iio_chan->scan_index = scan_index;
  392. iio_chan->scan_type.sign = 's';
  393. iio_chan->scan_type.realbits = 64;
  394. iio_chan->scan_type.storagebits = 64;
  395. iio_chan->scan_type.endianness = IIO_LE;
  396. iio_chan->ext_info = scmi_iio_ext_info;
  397. }
  398. static int scmi_iio_get_chan_modifier(const char *name,
  399. enum iio_modifier *modifier)
  400. {
  401. char *pch, mod;
  402. if (!name)
  403. return -EINVAL;
  404. pch = strrchr(name, '_');
  405. if (!pch)
  406. return -EINVAL;
  407. mod = *(pch + 1);
  408. switch (mod) {
  409. case 'X':
  410. *modifier = IIO_MOD_X;
  411. return 0;
  412. case 'Y':
  413. *modifier = IIO_MOD_Y;
  414. return 0;
  415. case 'Z':
  416. *modifier = IIO_MOD_Z;
  417. return 0;
  418. default:
  419. return -EINVAL;
  420. }
  421. }
  422. static int scmi_iio_get_chan_type(u8 scmi_type, enum iio_chan_type *iio_type)
  423. {
  424. switch (scmi_type) {
  425. case METERS_SEC_SQUARED:
  426. *iio_type = IIO_ACCEL;
  427. return 0;
  428. case RADIANS_SEC:
  429. *iio_type = IIO_ANGL_VEL;
  430. return 0;
  431. default:
  432. return -EINVAL;
  433. }
  434. }
  435. static u64 scmi_iio_convert_interval_to_ns(u32 val)
  436. {
  437. u64 sensor_update_interval =
  438. SCMI_SENS_INTVL_GET_SECS(val) * NSEC_PER_SEC;
  439. u64 sensor_interval_mult;
  440. int mult;
  441. mult = SCMI_SENS_INTVL_GET_EXP(val);
  442. if (mult < 0) {
  443. sensor_interval_mult = int_pow(10, abs(mult));
  444. do_div(sensor_update_interval, sensor_interval_mult);
  445. } else {
  446. sensor_interval_mult = int_pow(10, mult);
  447. sensor_update_interval =
  448. sensor_update_interval * sensor_interval_mult;
  449. }
  450. return sensor_update_interval;
  451. }
  452. static int scmi_iio_set_sampling_freq_avail(struct iio_dev *iio_dev)
  453. {
  454. u64 cur_interval_ns, low_interval_ns, high_interval_ns, step_size_ns,
  455. hz, uhz;
  456. unsigned int cur_interval, low_interval, high_interval, step_size;
  457. struct scmi_iio_priv *sensor = iio_priv(iio_dev);
  458. int i;
  459. sensor->freq_avail =
  460. devm_kzalloc(&iio_dev->dev,
  461. sizeof(*sensor->freq_avail) *
  462. (sensor->sensor_info->intervals.count * 2),
  463. GFP_KERNEL);
  464. if (!sensor->freq_avail)
  465. return -ENOMEM;
  466. if (sensor->sensor_info->intervals.segmented) {
  467. low_interval = sensor->sensor_info->intervals
  468. .desc[SCMI_SENS_INTVL_SEGMENT_LOW];
  469. low_interval_ns = scmi_iio_convert_interval_to_ns(low_interval);
  470. convert_ns_to_freq(low_interval_ns, &hz, &uhz);
  471. sensor->freq_avail[0] = hz;
  472. sensor->freq_avail[1] = uhz;
  473. step_size = sensor->sensor_info->intervals
  474. .desc[SCMI_SENS_INTVL_SEGMENT_STEP];
  475. step_size_ns = scmi_iio_convert_interval_to_ns(step_size);
  476. convert_ns_to_freq(step_size_ns, &hz, &uhz);
  477. sensor->freq_avail[2] = hz;
  478. sensor->freq_avail[3] = uhz;
  479. high_interval = sensor->sensor_info->intervals
  480. .desc[SCMI_SENS_INTVL_SEGMENT_HIGH];
  481. high_interval_ns =
  482. scmi_iio_convert_interval_to_ns(high_interval);
  483. convert_ns_to_freq(high_interval_ns, &hz, &uhz);
  484. sensor->freq_avail[4] = hz;
  485. sensor->freq_avail[5] = uhz;
  486. } else {
  487. for (i = 0; i < sensor->sensor_info->intervals.count; i++) {
  488. cur_interval = sensor->sensor_info->intervals.desc[i];
  489. cur_interval_ns =
  490. scmi_iio_convert_interval_to_ns(cur_interval);
  491. convert_ns_to_freq(cur_interval_ns, &hz, &uhz);
  492. sensor->freq_avail[i * 2] = hz;
  493. sensor->freq_avail[i * 2 + 1] = uhz;
  494. }
  495. }
  496. return 0;
  497. }
  498. static struct iio_dev *
  499. scmi_alloc_iiodev(struct scmi_device *sdev,
  500. const struct scmi_sensor_proto_ops *ops,
  501. struct scmi_protocol_handle *ph,
  502. const struct scmi_sensor_info *sensor_info)
  503. {
  504. struct iio_chan_spec *iio_channels;
  505. struct scmi_iio_priv *sensor;
  506. enum iio_modifier modifier;
  507. enum iio_chan_type type;
  508. struct iio_dev *iiodev;
  509. struct device *dev = &sdev->dev;
  510. const struct scmi_handle *handle = sdev->handle;
  511. int i, ret;
  512. iiodev = devm_iio_device_alloc(dev, sizeof(*sensor));
  513. if (!iiodev)
  514. return ERR_PTR(-ENOMEM);
  515. iiodev->modes = INDIO_DIRECT_MODE;
  516. sensor = iio_priv(iiodev);
  517. sensor->sensor_ops = ops;
  518. sensor->ph = ph;
  519. sensor->sensor_info = sensor_info;
  520. sensor->sensor_update_nb.notifier_call = scmi_iio_sensor_update_cb;
  521. sensor->indio_dev = iiodev;
  522. mutex_init(&sensor->lock);
  523. /* adding one additional channel for timestamp */
  524. iiodev->num_channels = sensor_info->num_axis + 1;
  525. iiodev->name = sensor_info->name;
  526. iiodev->info = &scmi_iio_info;
  527. iio_channels =
  528. devm_kzalloc(dev,
  529. sizeof(*iio_channels) * (iiodev->num_channels),
  530. GFP_KERNEL);
  531. if (!iio_channels)
  532. return ERR_PTR(-ENOMEM);
  533. ret = scmi_iio_set_sampling_freq_avail(iiodev);
  534. if (ret < 0)
  535. return ERR_PTR(ret);
  536. for (i = 0; i < sensor_info->num_axis; i++) {
  537. ret = scmi_iio_get_chan_type(sensor_info->axis[i].type, &type);
  538. if (ret < 0)
  539. return ERR_PTR(ret);
  540. ret = scmi_iio_get_chan_modifier(sensor_info->axis[i].name,
  541. &modifier);
  542. if (ret < 0)
  543. return ERR_PTR(ret);
  544. scmi_iio_set_data_channel(&iio_channels[i], type, modifier,
  545. sensor_info->axis[i].id);
  546. }
  547. ret = handle->notify_ops->devm_event_notifier_register(sdev,
  548. SCMI_PROTOCOL_SENSOR, SCMI_EVENT_SENSOR_UPDATE,
  549. &sensor->sensor_info->id,
  550. &sensor->sensor_update_nb);
  551. if (ret)
  552. return dev_err_ptr_probe(&iiodev->dev, ret,
  553. "Error in registering sensor update notifier for sensor %s\n",
  554. sensor->sensor_info->name);
  555. scmi_iio_set_timestamp_channel(&iio_channels[i], i);
  556. iiodev->channels = iio_channels;
  557. return iiodev;
  558. }
  559. static int scmi_iio_dev_probe(struct scmi_device *sdev)
  560. {
  561. const struct scmi_sensor_info *sensor_info;
  562. struct scmi_handle *handle = sdev->handle;
  563. const struct scmi_sensor_proto_ops *sensor_ops;
  564. struct scmi_protocol_handle *ph;
  565. struct device *dev = &sdev->dev;
  566. struct iio_dev *scmi_iio_dev;
  567. u16 nr_sensors;
  568. int err = -ENODEV, i;
  569. if (!handle)
  570. return -ENODEV;
  571. sensor_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_SENSOR, &ph);
  572. if (IS_ERR(sensor_ops))
  573. return dev_err_probe(dev, PTR_ERR(sensor_ops),
  574. "SCMI device has no sensor interface\n");
  575. nr_sensors = sensor_ops->count_get(ph);
  576. if (!nr_sensors) {
  577. dev_dbg(dev, "0 sensors found via SCMI bus\n");
  578. return -ENODEV;
  579. }
  580. for (i = 0; i < nr_sensors; i++) {
  581. sensor_info = sensor_ops->info_get(ph, i);
  582. if (!sensor_info) {
  583. return dev_err_probe(dev, -EINVAL,
  584. "SCMI sensor %d has missing info\n", i);
  585. }
  586. /* This driver only supports 3-axis accel and gyro, skipping other sensors */
  587. if (sensor_info->num_axis != SCMI_IIO_NUM_OF_AXIS)
  588. continue;
  589. /* This driver only supports 3-axis accel and gyro, skipping other sensors */
  590. if (sensor_info->axis[0].type != METERS_SEC_SQUARED &&
  591. sensor_info->axis[0].type != RADIANS_SEC)
  592. continue;
  593. scmi_iio_dev = scmi_alloc_iiodev(sdev, sensor_ops, ph,
  594. sensor_info);
  595. if (IS_ERR(scmi_iio_dev)) {
  596. return dev_err_probe(dev, PTR_ERR(scmi_iio_dev),
  597. "failed to allocate IIO device for sensor %s\n",
  598. sensor_info->name);
  599. }
  600. err = devm_iio_kfifo_buffer_setup(&scmi_iio_dev->dev,
  601. scmi_iio_dev,
  602. &scmi_iio_buffer_ops);
  603. if (err < 0) {
  604. return dev_err_probe(dev, err,
  605. "IIO buffer setup error at sensor %s\n",
  606. sensor_info->name);
  607. }
  608. err = devm_iio_device_register(dev, scmi_iio_dev);
  609. if (err)
  610. return dev_err_probe(dev, err,
  611. "IIO device registration failed at sensor %s\n",
  612. sensor_info->name);
  613. }
  614. return err;
  615. }
  616. static const struct scmi_device_id scmi_id_table[] = {
  617. { SCMI_PROTOCOL_SENSOR, "iiodev" },
  618. {},
  619. };
  620. MODULE_DEVICE_TABLE(scmi, scmi_id_table);
  621. static struct scmi_driver scmi_iiodev_driver = {
  622. .name = "scmi-sensor-iiodev",
  623. .probe = scmi_iio_dev_probe,
  624. .id_table = scmi_id_table,
  625. };
  626. module_scmi_driver(scmi_iiodev_driver);
  627. MODULE_AUTHOR("Jyoti Bhayana <jbhayana@google.com>");
  628. MODULE_DESCRIPTION("SCMI IIO Driver");
  629. MODULE_LICENSE("GPL v2");