sensors.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Sensor Protocol
  4. *
  5. * Copyright (C) 2018-2022 ARM Ltd.
  6. */
  7. #define pr_fmt(fmt) "SCMI Notifications SENSOR - " fmt
  8. #include <linux/bitfield.h>
  9. #include <linux/module.h>
  10. #include <linux/scmi_protocol.h>
  11. #include "protocols.h"
  12. #include "notify.h"
  13. #define SCMI_MAX_NUM_SENSOR_AXIS 63
  14. #define SCMIv2_SENSOR_PROTOCOL 0x10000
  15. enum scmi_sensor_protocol_cmd {
  16. SENSOR_DESCRIPTION_GET = 0x3,
  17. SENSOR_TRIP_POINT_NOTIFY = 0x4,
  18. SENSOR_TRIP_POINT_CONFIG = 0x5,
  19. SENSOR_READING_GET = 0x6,
  20. SENSOR_AXIS_DESCRIPTION_GET = 0x7,
  21. SENSOR_LIST_UPDATE_INTERVALS = 0x8,
  22. SENSOR_CONFIG_GET = 0x9,
  23. SENSOR_CONFIG_SET = 0xA,
  24. SENSOR_CONTINUOUS_UPDATE_NOTIFY = 0xB,
  25. SENSOR_NAME_GET = 0xC,
  26. SENSOR_AXIS_NAME_GET = 0xD,
  27. };
  28. struct scmi_msg_resp_sensor_attributes {
  29. __le16 num_sensors;
  30. u8 max_requests;
  31. u8 reserved;
  32. __le32 reg_addr_low;
  33. __le32 reg_addr_high;
  34. __le32 reg_size;
  35. };
  36. /* v3 attributes_low macros */
  37. #define SUPPORTS_UPDATE_NOTIFY(x) FIELD_GET(BIT(30), (x))
  38. #define SENSOR_TSTAMP_EXP(x) FIELD_GET(GENMASK(14, 10), (x))
  39. #define SUPPORTS_TIMESTAMP(x) FIELD_GET(BIT(9), (x))
  40. #define SUPPORTS_EXTEND_ATTRS(x) FIELD_GET(BIT(8), (x))
  41. /* v2 attributes_high macros */
  42. #define SENSOR_UPDATE_BASE(x) FIELD_GET(GENMASK(31, 27), (x))
  43. #define SENSOR_UPDATE_SCALE(x) FIELD_GET(GENMASK(26, 22), (x))
  44. /* v3 attributes_high macros */
  45. #define SENSOR_AXIS_NUMBER(x) FIELD_GET(GENMASK(21, 16), (x))
  46. #define SUPPORTS_AXIS(x) FIELD_GET(BIT(8), (x))
  47. /* v3 resolution macros */
  48. #define SENSOR_RES(x) FIELD_GET(GENMASK(26, 0), (x))
  49. #define SENSOR_RES_EXP(x) FIELD_GET(GENMASK(31, 27), (x))
  50. struct scmi_msg_resp_attrs {
  51. __le32 min_range_low;
  52. __le32 min_range_high;
  53. __le32 max_range_low;
  54. __le32 max_range_high;
  55. };
  56. struct scmi_msg_sensor_description {
  57. __le32 desc_index;
  58. };
  59. struct scmi_msg_resp_sensor_description {
  60. __le16 num_returned;
  61. __le16 num_remaining;
  62. struct scmi_sensor_descriptor {
  63. __le32 id;
  64. __le32 attributes_low;
  65. /* Common attributes_low macros */
  66. #define SUPPORTS_ASYNC_READ(x) FIELD_GET(BIT(31), (x))
  67. #define SUPPORTS_EXTENDED_NAMES(x) FIELD_GET(BIT(29), (x))
  68. #define NUM_TRIP_POINTS(x) FIELD_GET(GENMASK(7, 0), (x))
  69. __le32 attributes_high;
  70. /* Common attributes_high macros */
  71. #define SENSOR_SCALE(x) FIELD_GET(GENMASK(15, 11), (x))
  72. #define SENSOR_SCALE_SIGN BIT(4)
  73. #define SENSOR_SCALE_EXTEND GENMASK(31, 5)
  74. #define SENSOR_TYPE(x) FIELD_GET(GENMASK(7, 0), (x))
  75. u8 name[SCMI_SHORT_NAME_MAX_SIZE];
  76. /* only for version > 2.0 */
  77. __le32 power;
  78. __le32 resolution;
  79. struct scmi_msg_resp_attrs scalar_attrs;
  80. } desc[];
  81. };
  82. /* Base scmi_sensor_descriptor size excluding extended attrs after name */
  83. #define SCMI_MSG_RESP_SENS_DESCR_BASE_SZ 28
  84. /* Sign extend to a full s32 */
  85. #define S32_EXT(v) \
  86. ({ \
  87. int __v = (v); \
  88. \
  89. if (__v & SENSOR_SCALE_SIGN) \
  90. __v |= SENSOR_SCALE_EXTEND; \
  91. __v; \
  92. })
  93. struct scmi_msg_sensor_axis_description_get {
  94. __le32 id;
  95. __le32 axis_desc_index;
  96. };
  97. struct scmi_msg_resp_sensor_axis_description {
  98. __le32 num_axis_flags;
  99. #define NUM_AXIS_RETURNED(x) FIELD_GET(GENMASK(5, 0), (x))
  100. #define NUM_AXIS_REMAINING(x) FIELD_GET(GENMASK(31, 26), (x))
  101. struct scmi_axis_descriptor {
  102. __le32 id;
  103. __le32 attributes_low;
  104. #define SUPPORTS_EXTENDED_AXIS_NAMES(x) FIELD_GET(BIT(9), (x))
  105. __le32 attributes_high;
  106. u8 name[SCMI_SHORT_NAME_MAX_SIZE];
  107. __le32 resolution;
  108. struct scmi_msg_resp_attrs attrs;
  109. } desc[];
  110. };
  111. struct scmi_msg_resp_sensor_axis_names_description {
  112. __le32 num_axis_flags;
  113. struct scmi_sensor_axis_name_descriptor {
  114. __le32 axis_id;
  115. u8 name[SCMI_MAX_STR_SIZE];
  116. } desc[];
  117. };
  118. /* Base scmi_axis_descriptor size excluding extended attrs after name */
  119. #define SCMI_MSG_RESP_AXIS_DESCR_BASE_SZ 28
  120. struct scmi_msg_sensor_list_update_intervals {
  121. __le32 id;
  122. __le32 index;
  123. };
  124. struct scmi_msg_resp_sensor_list_update_intervals {
  125. __le32 num_intervals_flags;
  126. #define NUM_INTERVALS_RETURNED(x) FIELD_GET(GENMASK(11, 0), (x))
  127. #define SEGMENTED_INTVL_FORMAT(x) FIELD_GET(BIT(12), (x))
  128. #define NUM_INTERVALS_REMAINING(x) FIELD_GET(GENMASK(31, 16), (x))
  129. __le32 intervals[];
  130. };
  131. struct scmi_msg_sensor_request_notify {
  132. __le32 id;
  133. __le32 event_control;
  134. #define SENSOR_NOTIFY_ALL BIT(0)
  135. };
  136. struct scmi_msg_set_sensor_trip_point {
  137. __le32 id;
  138. __le32 event_control;
  139. #define SENSOR_TP_EVENT_MASK (0x3)
  140. #define SENSOR_TP_DISABLED 0x0
  141. #define SENSOR_TP_POSITIVE 0x1
  142. #define SENSOR_TP_NEGATIVE 0x2
  143. #define SENSOR_TP_BOTH 0x3
  144. #define SENSOR_TP_ID(x) (((x) & 0xff) << 4)
  145. __le32 value_low;
  146. __le32 value_high;
  147. };
  148. struct scmi_msg_sensor_config_set {
  149. __le32 id;
  150. __le32 sensor_config;
  151. };
  152. struct scmi_msg_sensor_reading_get {
  153. __le32 id;
  154. __le32 flags;
  155. #define SENSOR_READ_ASYNC BIT(0)
  156. };
  157. struct scmi_resp_sensor_reading_complete {
  158. __le32 id;
  159. __le32 readings_low;
  160. __le32 readings_high;
  161. };
  162. struct scmi_sensor_reading_resp {
  163. __le32 sensor_value_low;
  164. __le32 sensor_value_high;
  165. __le32 timestamp_low;
  166. __le32 timestamp_high;
  167. };
  168. struct scmi_resp_sensor_reading_complete_v3 {
  169. __le32 id;
  170. struct scmi_sensor_reading_resp readings[];
  171. };
  172. struct scmi_sensor_trip_notify_payld {
  173. __le32 agent_id;
  174. __le32 sensor_id;
  175. __le32 trip_point_desc;
  176. };
  177. struct scmi_sensor_update_notify_payld {
  178. __le32 agent_id;
  179. __le32 sensor_id;
  180. struct scmi_sensor_reading_resp readings[];
  181. };
  182. struct sensors_info {
  183. u32 version;
  184. int num_sensors;
  185. int max_requests;
  186. u64 reg_addr;
  187. u32 reg_size;
  188. struct scmi_sensor_info *sensors;
  189. };
  190. static int scmi_sensor_attributes_get(const struct scmi_protocol_handle *ph,
  191. struct sensors_info *si)
  192. {
  193. int ret;
  194. struct scmi_xfer *t;
  195. struct scmi_msg_resp_sensor_attributes *attr;
  196. ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
  197. 0, sizeof(*attr), &t);
  198. if (ret)
  199. return ret;
  200. attr = t->rx.buf;
  201. ret = ph->xops->do_xfer(ph, t);
  202. if (!ret) {
  203. si->num_sensors = le16_to_cpu(attr->num_sensors);
  204. si->max_requests = attr->max_requests;
  205. si->reg_addr = le32_to_cpu(attr->reg_addr_low) |
  206. (u64)le32_to_cpu(attr->reg_addr_high) << 32;
  207. si->reg_size = le32_to_cpu(attr->reg_size);
  208. }
  209. ph->xops->xfer_put(ph, t);
  210. return ret;
  211. }
  212. static inline void scmi_parse_range_attrs(struct scmi_range_attrs *out,
  213. const struct scmi_msg_resp_attrs *in)
  214. {
  215. out->min_range = get_unaligned_le64((void *)&in->min_range_low);
  216. out->max_range = get_unaligned_le64((void *)&in->max_range_low);
  217. }
  218. struct scmi_sens_ipriv {
  219. void *priv;
  220. struct device *dev;
  221. };
  222. static void iter_intervals_prepare_message(void *message,
  223. unsigned int desc_index,
  224. const void *p)
  225. {
  226. struct scmi_msg_sensor_list_update_intervals *msg = message;
  227. const struct scmi_sensor_info *s;
  228. s = ((const struct scmi_sens_ipriv *)p)->priv;
  229. /* Set the number of sensors to be skipped/already read */
  230. msg->id = cpu_to_le32(s->id);
  231. msg->index = cpu_to_le32(desc_index);
  232. }
  233. static int iter_intervals_update_state(struct scmi_iterator_state *st,
  234. const void *response, void *p)
  235. {
  236. u32 flags;
  237. struct scmi_sensor_info *s = ((struct scmi_sens_ipriv *)p)->priv;
  238. struct device *dev = ((struct scmi_sens_ipriv *)p)->dev;
  239. const struct scmi_msg_resp_sensor_list_update_intervals *r = response;
  240. flags = le32_to_cpu(r->num_intervals_flags);
  241. st->num_returned = NUM_INTERVALS_RETURNED(flags);
  242. st->num_remaining = NUM_INTERVALS_REMAINING(flags);
  243. /*
  244. * Max intervals is not declared previously anywhere so we
  245. * assume it's returned+remaining on first call.
  246. */
  247. if (!st->max_resources) {
  248. s->intervals.segmented = SEGMENTED_INTVL_FORMAT(flags);
  249. s->intervals.count = st->num_returned + st->num_remaining;
  250. /* segmented intervals are reported in one triplet */
  251. if (s->intervals.segmented &&
  252. (st->num_remaining || st->num_returned != 3)) {
  253. dev_err(dev,
  254. "Sensor ID:%d advertises an invalid segmented interval (%d)\n",
  255. s->id, s->intervals.count);
  256. s->intervals.segmented = false;
  257. s->intervals.count = 0;
  258. return -EINVAL;
  259. }
  260. /* Direct allocation when exceeding pre-allocated */
  261. if (s->intervals.count >= SCMI_MAX_PREALLOC_POOL) {
  262. s->intervals.desc =
  263. devm_kcalloc(dev,
  264. s->intervals.count,
  265. sizeof(*s->intervals.desc),
  266. GFP_KERNEL);
  267. if (!s->intervals.desc) {
  268. s->intervals.segmented = false;
  269. s->intervals.count = 0;
  270. return -ENOMEM;
  271. }
  272. }
  273. st->max_resources = s->intervals.count;
  274. }
  275. return 0;
  276. }
  277. static int
  278. iter_intervals_process_response(const struct scmi_protocol_handle *ph,
  279. const void *response,
  280. struct scmi_iterator_state *st, void *p)
  281. {
  282. const struct scmi_msg_resp_sensor_list_update_intervals *r = response;
  283. struct scmi_sensor_info *s = ((struct scmi_sens_ipriv *)p)->priv;
  284. s->intervals.desc[st->desc_index + st->loop_idx] =
  285. le32_to_cpu(r->intervals[st->loop_idx]);
  286. return 0;
  287. }
  288. static int scmi_sensor_update_intervals(const struct scmi_protocol_handle *ph,
  289. struct scmi_sensor_info *s)
  290. {
  291. void *iter;
  292. struct scmi_iterator_ops ops = {
  293. .prepare_message = iter_intervals_prepare_message,
  294. .update_state = iter_intervals_update_state,
  295. .process_response = iter_intervals_process_response,
  296. };
  297. struct scmi_sens_ipriv upriv = {
  298. .priv = s,
  299. .dev = ph->dev,
  300. };
  301. iter = ph->hops->iter_response_init(ph, &ops, s->intervals.count,
  302. SENSOR_LIST_UPDATE_INTERVALS,
  303. sizeof(struct scmi_msg_sensor_list_update_intervals),
  304. &upriv);
  305. if (IS_ERR(iter))
  306. return PTR_ERR(iter);
  307. return ph->hops->iter_response_run(iter);
  308. }
  309. struct scmi_apriv {
  310. bool any_axes_support_extended_names;
  311. struct scmi_sensor_info *s;
  312. };
  313. static void iter_axes_desc_prepare_message(void *message,
  314. const unsigned int desc_index,
  315. const void *priv)
  316. {
  317. struct scmi_msg_sensor_axis_description_get *msg = message;
  318. const struct scmi_apriv *apriv = priv;
  319. /* Set the number of sensors to be skipped/already read */
  320. msg->id = cpu_to_le32(apriv->s->id);
  321. msg->axis_desc_index = cpu_to_le32(desc_index);
  322. }
  323. static int
  324. iter_axes_desc_update_state(struct scmi_iterator_state *st,
  325. const void *response, void *priv)
  326. {
  327. u32 flags;
  328. const struct scmi_msg_resp_sensor_axis_description *r = response;
  329. flags = le32_to_cpu(r->num_axis_flags);
  330. st->num_returned = NUM_AXIS_RETURNED(flags);
  331. st->num_remaining = NUM_AXIS_REMAINING(flags);
  332. st->priv = (void *)&r->desc[0];
  333. return 0;
  334. }
  335. static int
  336. iter_axes_desc_process_response(const struct scmi_protocol_handle *ph,
  337. const void *response,
  338. struct scmi_iterator_state *st, void *priv)
  339. {
  340. u32 attrh, attrl;
  341. struct scmi_sensor_axis_info *a;
  342. size_t dsize = SCMI_MSG_RESP_AXIS_DESCR_BASE_SZ;
  343. struct scmi_apriv *apriv = priv;
  344. const struct scmi_axis_descriptor *adesc = st->priv;
  345. attrl = le32_to_cpu(adesc->attributes_low);
  346. if (SUPPORTS_EXTENDED_AXIS_NAMES(attrl))
  347. apriv->any_axes_support_extended_names = true;
  348. a = &apriv->s->axis[st->desc_index + st->loop_idx];
  349. a->id = le32_to_cpu(adesc->id);
  350. a->extended_attrs = SUPPORTS_EXTEND_ATTRS(attrl);
  351. attrh = le32_to_cpu(adesc->attributes_high);
  352. a->scale = S32_EXT(SENSOR_SCALE(attrh));
  353. a->type = SENSOR_TYPE(attrh);
  354. strscpy(a->name, adesc->name, SCMI_SHORT_NAME_MAX_SIZE);
  355. if (a->extended_attrs) {
  356. unsigned int ares = le32_to_cpu(adesc->resolution);
  357. a->resolution = SENSOR_RES(ares);
  358. a->exponent = S32_EXT(SENSOR_RES_EXP(ares));
  359. dsize += sizeof(adesc->resolution);
  360. scmi_parse_range_attrs(&a->attrs, &adesc->attrs);
  361. dsize += sizeof(adesc->attrs);
  362. }
  363. st->priv = ((u8 *)adesc + dsize);
  364. return 0;
  365. }
  366. static int
  367. iter_axes_extended_name_update_state(struct scmi_iterator_state *st,
  368. const void *response, void *priv)
  369. {
  370. u32 flags;
  371. const struct scmi_msg_resp_sensor_axis_names_description *r = response;
  372. flags = le32_to_cpu(r->num_axis_flags);
  373. st->num_returned = NUM_AXIS_RETURNED(flags);
  374. st->num_remaining = NUM_AXIS_REMAINING(flags);
  375. st->priv = (void *)&r->desc[0];
  376. return 0;
  377. }
  378. static int
  379. iter_axes_extended_name_process_response(const struct scmi_protocol_handle *ph,
  380. const void *response,
  381. struct scmi_iterator_state *st,
  382. void *priv)
  383. {
  384. struct scmi_sensor_axis_info *a;
  385. const struct scmi_apriv *apriv = priv;
  386. struct scmi_sensor_axis_name_descriptor *adesc = st->priv;
  387. u32 axis_id = le32_to_cpu(adesc->axis_id);
  388. if (axis_id >= st->max_resources)
  389. return -EPROTO;
  390. /*
  391. * Pick the corresponding descriptor based on the axis_id embedded
  392. * in the reply since the list of axes supporting extended names
  393. * can be a subset of all the axes.
  394. */
  395. a = &apriv->s->axis[axis_id];
  396. strscpy(a->name, adesc->name, SCMI_MAX_STR_SIZE);
  397. st->priv = ++adesc;
  398. return 0;
  399. }
  400. static int
  401. scmi_sensor_axis_extended_names_get(const struct scmi_protocol_handle *ph,
  402. struct scmi_sensor_info *s)
  403. {
  404. int ret;
  405. void *iter;
  406. struct scmi_iterator_ops ops = {
  407. .prepare_message = iter_axes_desc_prepare_message,
  408. .update_state = iter_axes_extended_name_update_state,
  409. .process_response = iter_axes_extended_name_process_response,
  410. };
  411. struct scmi_apriv apriv = {
  412. .any_axes_support_extended_names = false,
  413. .s = s,
  414. };
  415. iter = ph->hops->iter_response_init(ph, &ops, s->num_axis,
  416. SENSOR_AXIS_NAME_GET,
  417. sizeof(struct scmi_msg_sensor_axis_description_get),
  418. &apriv);
  419. if (IS_ERR(iter))
  420. return PTR_ERR(iter);
  421. /*
  422. * Do not cause whole protocol initialization failure when failing to
  423. * get extended names for axes.
  424. */
  425. ret = ph->hops->iter_response_run(iter);
  426. if (ret)
  427. dev_warn(ph->dev,
  428. "Failed to get axes extended names for %s (ret:%d).\n",
  429. s->name, ret);
  430. return 0;
  431. }
  432. static int scmi_sensor_axis_description(const struct scmi_protocol_handle *ph,
  433. struct scmi_sensor_info *s,
  434. u32 version)
  435. {
  436. int ret;
  437. void *iter;
  438. struct scmi_iterator_ops ops = {
  439. .prepare_message = iter_axes_desc_prepare_message,
  440. .update_state = iter_axes_desc_update_state,
  441. .process_response = iter_axes_desc_process_response,
  442. };
  443. struct scmi_apriv apriv = {
  444. .any_axes_support_extended_names = false,
  445. .s = s,
  446. };
  447. s->axis = devm_kcalloc(ph->dev, s->num_axis,
  448. sizeof(*s->axis), GFP_KERNEL);
  449. if (!s->axis)
  450. return -ENOMEM;
  451. iter = ph->hops->iter_response_init(ph, &ops, s->num_axis,
  452. SENSOR_AXIS_DESCRIPTION_GET,
  453. sizeof(struct scmi_msg_sensor_axis_description_get),
  454. &apriv);
  455. if (IS_ERR(iter))
  456. return PTR_ERR(iter);
  457. ret = ph->hops->iter_response_run(iter);
  458. if (ret)
  459. return ret;
  460. if (PROTOCOL_REV_MAJOR(version) >= 0x3 &&
  461. apriv.any_axes_support_extended_names)
  462. ret = scmi_sensor_axis_extended_names_get(ph, s);
  463. return ret;
  464. }
  465. static void iter_sens_descr_prepare_message(void *message,
  466. unsigned int desc_index,
  467. const void *priv)
  468. {
  469. struct scmi_msg_sensor_description *msg = message;
  470. msg->desc_index = cpu_to_le32(desc_index);
  471. }
  472. static int iter_sens_descr_update_state(struct scmi_iterator_state *st,
  473. const void *response, void *priv)
  474. {
  475. const struct scmi_msg_resp_sensor_description *r = response;
  476. st->num_returned = le16_to_cpu(r->num_returned);
  477. st->num_remaining = le16_to_cpu(r->num_remaining);
  478. st->priv = (void *)&r->desc[0];
  479. return 0;
  480. }
  481. static int
  482. iter_sens_descr_process_response(const struct scmi_protocol_handle *ph,
  483. const void *response,
  484. struct scmi_iterator_state *st, void *priv)
  485. {
  486. int ret = 0;
  487. u32 attrh, attrl;
  488. size_t dsize = SCMI_MSG_RESP_SENS_DESCR_BASE_SZ;
  489. struct scmi_sensor_info *s;
  490. struct sensors_info *si = priv;
  491. const struct scmi_sensor_descriptor *sdesc = st->priv;
  492. s = &si->sensors[st->desc_index + st->loop_idx];
  493. s->id = le32_to_cpu(sdesc->id);
  494. attrl = le32_to_cpu(sdesc->attributes_low);
  495. /* common bitfields parsing */
  496. s->async = SUPPORTS_ASYNC_READ(attrl);
  497. s->num_trip_points = NUM_TRIP_POINTS(attrl);
  498. /**
  499. * only SCMIv3.0 specific bitfield below.
  500. * Such bitfields are assumed to be zeroed on non
  501. * relevant fw versions...assuming fw not buggy !
  502. */
  503. s->update = SUPPORTS_UPDATE_NOTIFY(attrl);
  504. s->timestamped = SUPPORTS_TIMESTAMP(attrl);
  505. if (s->timestamped)
  506. s->tstamp_scale = S32_EXT(SENSOR_TSTAMP_EXP(attrl));
  507. s->extended_scalar_attrs = SUPPORTS_EXTEND_ATTRS(attrl);
  508. attrh = le32_to_cpu(sdesc->attributes_high);
  509. /* common bitfields parsing */
  510. s->scale = S32_EXT(SENSOR_SCALE(attrh));
  511. s->type = SENSOR_TYPE(attrh);
  512. /* Use pre-allocated pool wherever possible */
  513. s->intervals.desc = s->intervals.prealloc_pool;
  514. if (si->version == SCMIv2_SENSOR_PROTOCOL) {
  515. s->intervals.segmented = false;
  516. s->intervals.count = 1;
  517. /*
  518. * Convert SCMIv2.0 update interval format to
  519. * SCMIv3.0 to be used as the common exposed
  520. * descriptor, accessible via common macros.
  521. */
  522. s->intervals.desc[0] = (SENSOR_UPDATE_BASE(attrh) << 5) |
  523. SENSOR_UPDATE_SCALE(attrh);
  524. } else {
  525. /*
  526. * From SCMIv3.0 update intervals are retrieved
  527. * via a dedicated (optional) command.
  528. * Since the command is optional, on error carry
  529. * on without any update interval.
  530. */
  531. if (scmi_sensor_update_intervals(ph, s))
  532. dev_dbg(ph->dev,
  533. "Update Intervals not available for sensor ID:%d\n",
  534. s->id);
  535. }
  536. /**
  537. * only > SCMIv2.0 specific bitfield below.
  538. * Such bitfields are assumed to be zeroed on non
  539. * relevant fw versions...assuming fw not buggy !
  540. */
  541. s->num_axis = min_t(unsigned int,
  542. SUPPORTS_AXIS(attrh) ?
  543. SENSOR_AXIS_NUMBER(attrh) : 0,
  544. SCMI_MAX_NUM_SENSOR_AXIS);
  545. strscpy(s->name, sdesc->name, SCMI_SHORT_NAME_MAX_SIZE);
  546. /*
  547. * If supported overwrite short name with the extended
  548. * one; on error just carry on and use already provided
  549. * short name.
  550. */
  551. if (PROTOCOL_REV_MAJOR(si->version) >= 0x3 &&
  552. SUPPORTS_EXTENDED_NAMES(attrl))
  553. ph->hops->extended_name_get(ph, SENSOR_NAME_GET, s->id,
  554. s->name, SCMI_MAX_STR_SIZE);
  555. if (s->extended_scalar_attrs) {
  556. s->sensor_power = le32_to_cpu(sdesc->power);
  557. dsize += sizeof(sdesc->power);
  558. /* Only for sensors reporting scalar values */
  559. if (s->num_axis == 0) {
  560. unsigned int sres = le32_to_cpu(sdesc->resolution);
  561. s->resolution = SENSOR_RES(sres);
  562. s->exponent = S32_EXT(SENSOR_RES_EXP(sres));
  563. dsize += sizeof(sdesc->resolution);
  564. scmi_parse_range_attrs(&s->scalar_attrs,
  565. &sdesc->scalar_attrs);
  566. dsize += sizeof(sdesc->scalar_attrs);
  567. }
  568. }
  569. if (s->num_axis > 0)
  570. ret = scmi_sensor_axis_description(ph, s, si->version);
  571. st->priv = ((u8 *)sdesc + dsize);
  572. return ret;
  573. }
  574. static int scmi_sensor_description_get(const struct scmi_protocol_handle *ph,
  575. struct sensors_info *si)
  576. {
  577. void *iter;
  578. struct scmi_iterator_ops ops = {
  579. .prepare_message = iter_sens_descr_prepare_message,
  580. .update_state = iter_sens_descr_update_state,
  581. .process_response = iter_sens_descr_process_response,
  582. };
  583. iter = ph->hops->iter_response_init(ph, &ops, si->num_sensors,
  584. SENSOR_DESCRIPTION_GET,
  585. sizeof(__le32), si);
  586. if (IS_ERR(iter))
  587. return PTR_ERR(iter);
  588. return ph->hops->iter_response_run(iter);
  589. }
  590. static inline int
  591. scmi_sensor_request_notify(const struct scmi_protocol_handle *ph, u32 sensor_id,
  592. u8 message_id, bool enable)
  593. {
  594. int ret;
  595. u32 evt_cntl = enable ? SENSOR_NOTIFY_ALL : 0;
  596. struct scmi_xfer *t;
  597. struct scmi_msg_sensor_request_notify *cfg;
  598. ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*cfg), 0, &t);
  599. if (ret)
  600. return ret;
  601. cfg = t->tx.buf;
  602. cfg->id = cpu_to_le32(sensor_id);
  603. cfg->event_control = cpu_to_le32(evt_cntl);
  604. ret = ph->xops->do_xfer(ph, t);
  605. ph->xops->xfer_put(ph, t);
  606. return ret;
  607. }
  608. static int scmi_sensor_trip_point_notify(const struct scmi_protocol_handle *ph,
  609. u32 sensor_id, bool enable)
  610. {
  611. return scmi_sensor_request_notify(ph, sensor_id,
  612. SENSOR_TRIP_POINT_NOTIFY,
  613. enable);
  614. }
  615. static int
  616. scmi_sensor_continuous_update_notify(const struct scmi_protocol_handle *ph,
  617. u32 sensor_id, bool enable)
  618. {
  619. return scmi_sensor_request_notify(ph, sensor_id,
  620. SENSOR_CONTINUOUS_UPDATE_NOTIFY,
  621. enable);
  622. }
  623. static int
  624. scmi_sensor_trip_point_config(const struct scmi_protocol_handle *ph,
  625. u32 sensor_id, u8 trip_id, u64 trip_value)
  626. {
  627. int ret;
  628. u32 evt_cntl = SENSOR_TP_BOTH;
  629. struct scmi_xfer *t;
  630. struct scmi_msg_set_sensor_trip_point *trip;
  631. ret = ph->xops->xfer_get_init(ph, SENSOR_TRIP_POINT_CONFIG,
  632. sizeof(*trip), 0, &t);
  633. if (ret)
  634. return ret;
  635. trip = t->tx.buf;
  636. trip->id = cpu_to_le32(sensor_id);
  637. trip->event_control = cpu_to_le32(evt_cntl | SENSOR_TP_ID(trip_id));
  638. trip->value_low = cpu_to_le32(trip_value & 0xffffffff);
  639. trip->value_high = cpu_to_le32(trip_value >> 32);
  640. ret = ph->xops->do_xfer(ph, t);
  641. ph->xops->xfer_put(ph, t);
  642. return ret;
  643. }
  644. static int scmi_sensor_config_get(const struct scmi_protocol_handle *ph,
  645. u32 sensor_id, u32 *sensor_config)
  646. {
  647. int ret;
  648. struct scmi_xfer *t;
  649. struct sensors_info *si = ph->get_priv(ph);
  650. if (sensor_id >= si->num_sensors)
  651. return -EINVAL;
  652. ret = ph->xops->xfer_get_init(ph, SENSOR_CONFIG_GET,
  653. sizeof(__le32), sizeof(__le32), &t);
  654. if (ret)
  655. return ret;
  656. put_unaligned_le32(sensor_id, t->tx.buf);
  657. ret = ph->xops->do_xfer(ph, t);
  658. if (!ret) {
  659. struct scmi_sensor_info *s = si->sensors + sensor_id;
  660. *sensor_config = get_unaligned_le64(t->rx.buf);
  661. s->sensor_config = *sensor_config;
  662. }
  663. ph->xops->xfer_put(ph, t);
  664. return ret;
  665. }
  666. static int scmi_sensor_config_set(const struct scmi_protocol_handle *ph,
  667. u32 sensor_id, u32 sensor_config)
  668. {
  669. int ret;
  670. struct scmi_xfer *t;
  671. struct scmi_msg_sensor_config_set *msg;
  672. struct sensors_info *si = ph->get_priv(ph);
  673. if (sensor_id >= si->num_sensors)
  674. return -EINVAL;
  675. ret = ph->xops->xfer_get_init(ph, SENSOR_CONFIG_SET,
  676. sizeof(*msg), 0, &t);
  677. if (ret)
  678. return ret;
  679. msg = t->tx.buf;
  680. msg->id = cpu_to_le32(sensor_id);
  681. msg->sensor_config = cpu_to_le32(sensor_config);
  682. ret = ph->xops->do_xfer(ph, t);
  683. if (!ret) {
  684. struct scmi_sensor_info *s = si->sensors + sensor_id;
  685. s->sensor_config = sensor_config;
  686. }
  687. ph->xops->xfer_put(ph, t);
  688. return ret;
  689. }
  690. /**
  691. * scmi_sensor_reading_get - Read scalar sensor value
  692. * @ph: Protocol handle
  693. * @sensor_id: Sensor ID
  694. * @value: The 64bit value sensor reading
  695. *
  696. * This function returns a single 64 bit reading value representing the sensor
  697. * value; if the platform SCMI Protocol implementation and the sensor support
  698. * multiple axis and timestamped-reads, this just returns the first axis while
  699. * dropping the timestamp value.
  700. * Use instead the @scmi_sensor_reading_get_timestamped to retrieve the array of
  701. * timestamped multi-axis values.
  702. *
  703. * Return: 0 on Success
  704. */
  705. static int scmi_sensor_reading_get(const struct scmi_protocol_handle *ph,
  706. u32 sensor_id, u64 *value)
  707. {
  708. int ret;
  709. struct scmi_xfer *t;
  710. struct scmi_msg_sensor_reading_get *sensor;
  711. struct scmi_sensor_info *s;
  712. struct sensors_info *si = ph->get_priv(ph);
  713. if (sensor_id >= si->num_sensors)
  714. return -EINVAL;
  715. ret = ph->xops->xfer_get_init(ph, SENSOR_READING_GET,
  716. sizeof(*sensor), 0, &t);
  717. if (ret)
  718. return ret;
  719. sensor = t->tx.buf;
  720. sensor->id = cpu_to_le32(sensor_id);
  721. s = si->sensors + sensor_id;
  722. if (s->async) {
  723. sensor->flags = cpu_to_le32(SENSOR_READ_ASYNC);
  724. ret = ph->xops->do_xfer_with_response(ph, t);
  725. if (!ret) {
  726. struct scmi_resp_sensor_reading_complete *resp;
  727. resp = t->rx.buf;
  728. if (le32_to_cpu(resp->id) == sensor_id)
  729. *value =
  730. get_unaligned_le64(&resp->readings_low);
  731. else
  732. ret = -EPROTO;
  733. }
  734. } else {
  735. sensor->flags = cpu_to_le32(0);
  736. ret = ph->xops->do_xfer(ph, t);
  737. if (!ret)
  738. *value = get_unaligned_le64(t->rx.buf);
  739. }
  740. ph->xops->xfer_put(ph, t);
  741. return ret;
  742. }
  743. static inline void
  744. scmi_parse_sensor_readings(struct scmi_sensor_reading *out,
  745. const struct scmi_sensor_reading_resp *in)
  746. {
  747. out->value = get_unaligned_le64((void *)&in->sensor_value_low);
  748. out->timestamp = get_unaligned_le64((void *)&in->timestamp_low);
  749. }
  750. /**
  751. * scmi_sensor_reading_get_timestamped - Read multiple-axis timestamped values
  752. * @ph: Protocol handle
  753. * @sensor_id: Sensor ID
  754. * @count: The length of the provided @readings array
  755. * @readings: An array of elements each representing a timestamped per-axis
  756. * reading of type @struct scmi_sensor_reading.
  757. * Returned readings are ordered as the @axis descriptors array
  758. * included in @struct scmi_sensor_info and the max number of
  759. * returned elements is min(@count, @num_axis); ideally the provided
  760. * array should be of length @count equal to @num_axis.
  761. *
  762. * Return: 0 on Success
  763. */
  764. static int
  765. scmi_sensor_reading_get_timestamped(const struct scmi_protocol_handle *ph,
  766. u32 sensor_id, u8 count,
  767. struct scmi_sensor_reading *readings)
  768. {
  769. int ret;
  770. struct scmi_xfer *t;
  771. struct scmi_msg_sensor_reading_get *sensor;
  772. struct scmi_sensor_info *s;
  773. struct sensors_info *si = ph->get_priv(ph);
  774. if (sensor_id >= si->num_sensors)
  775. return -EINVAL;
  776. s = si->sensors + sensor_id;
  777. if (!count || !readings ||
  778. (!s->num_axis && count > 1) || (s->num_axis && count > s->num_axis))
  779. return -EINVAL;
  780. ret = ph->xops->xfer_get_init(ph, SENSOR_READING_GET,
  781. sizeof(*sensor), 0, &t);
  782. if (ret)
  783. return ret;
  784. sensor = t->tx.buf;
  785. sensor->id = cpu_to_le32(sensor_id);
  786. if (s->async) {
  787. sensor->flags = cpu_to_le32(SENSOR_READ_ASYNC);
  788. ret = ph->xops->do_xfer_with_response(ph, t);
  789. if (!ret) {
  790. int i;
  791. struct scmi_resp_sensor_reading_complete_v3 *resp;
  792. resp = t->rx.buf;
  793. /* Retrieve only the number of requested axis anyway */
  794. if (le32_to_cpu(resp->id) == sensor_id)
  795. for (i = 0; i < count; i++)
  796. scmi_parse_sensor_readings(&readings[i],
  797. &resp->readings[i]);
  798. else
  799. ret = -EPROTO;
  800. }
  801. } else {
  802. sensor->flags = cpu_to_le32(0);
  803. ret = ph->xops->do_xfer(ph, t);
  804. if (!ret) {
  805. int i;
  806. struct scmi_sensor_reading_resp *resp_readings;
  807. resp_readings = t->rx.buf;
  808. for (i = 0; i < count; i++)
  809. scmi_parse_sensor_readings(&readings[i],
  810. &resp_readings[i]);
  811. }
  812. }
  813. ph->xops->xfer_put(ph, t);
  814. return ret;
  815. }
  816. static const struct scmi_sensor_info *
  817. scmi_sensor_info_get(const struct scmi_protocol_handle *ph, u32 sensor_id)
  818. {
  819. struct sensors_info *si = ph->get_priv(ph);
  820. if (sensor_id >= si->num_sensors)
  821. return NULL;
  822. return si->sensors + sensor_id;
  823. }
  824. static int scmi_sensor_count_get(const struct scmi_protocol_handle *ph)
  825. {
  826. struct sensors_info *si = ph->get_priv(ph);
  827. return si->num_sensors;
  828. }
  829. static const struct scmi_sensor_proto_ops sensor_proto_ops = {
  830. .count_get = scmi_sensor_count_get,
  831. .info_get = scmi_sensor_info_get,
  832. .trip_point_config = scmi_sensor_trip_point_config,
  833. .reading_get = scmi_sensor_reading_get,
  834. .reading_get_timestamped = scmi_sensor_reading_get_timestamped,
  835. .config_get = scmi_sensor_config_get,
  836. .config_set = scmi_sensor_config_set,
  837. };
  838. static int scmi_sensor_set_notify_enabled(const struct scmi_protocol_handle *ph,
  839. u8 evt_id, u32 src_id, bool enable)
  840. {
  841. int ret;
  842. switch (evt_id) {
  843. case SCMI_EVENT_SENSOR_TRIP_POINT_EVENT:
  844. ret = scmi_sensor_trip_point_notify(ph, src_id, enable);
  845. break;
  846. case SCMI_EVENT_SENSOR_UPDATE:
  847. ret = scmi_sensor_continuous_update_notify(ph, src_id, enable);
  848. break;
  849. default:
  850. ret = -EINVAL;
  851. break;
  852. }
  853. if (ret)
  854. pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
  855. evt_id, src_id, ret);
  856. return ret;
  857. }
  858. static void *
  859. scmi_sensor_fill_custom_report(const struct scmi_protocol_handle *ph,
  860. u8 evt_id, ktime_t timestamp,
  861. const void *payld, size_t payld_sz,
  862. void *report, u32 *src_id)
  863. {
  864. void *rep = NULL;
  865. switch (evt_id) {
  866. case SCMI_EVENT_SENSOR_TRIP_POINT_EVENT:
  867. {
  868. const struct scmi_sensor_trip_notify_payld *p = payld;
  869. struct scmi_sensor_trip_point_report *r = report;
  870. if (sizeof(*p) != payld_sz)
  871. break;
  872. r->timestamp = timestamp;
  873. r->agent_id = le32_to_cpu(p->agent_id);
  874. r->sensor_id = le32_to_cpu(p->sensor_id);
  875. r->trip_point_desc = le32_to_cpu(p->trip_point_desc);
  876. *src_id = r->sensor_id;
  877. rep = r;
  878. break;
  879. }
  880. case SCMI_EVENT_SENSOR_UPDATE:
  881. {
  882. int i;
  883. struct scmi_sensor_info *s;
  884. const struct scmi_sensor_update_notify_payld *p = payld;
  885. struct scmi_sensor_update_report *r = report;
  886. struct sensors_info *sinfo = ph->get_priv(ph);
  887. /* payld_sz is variable for this event */
  888. r->sensor_id = le32_to_cpu(p->sensor_id);
  889. if (r->sensor_id >= sinfo->num_sensors)
  890. break;
  891. r->timestamp = timestamp;
  892. r->agent_id = le32_to_cpu(p->agent_id);
  893. s = &sinfo->sensors[r->sensor_id];
  894. /*
  895. * The generated report r (@struct scmi_sensor_update_report)
  896. * was pre-allocated to contain up to SCMI_MAX_NUM_SENSOR_AXIS
  897. * readings: here it is filled with the effective @num_axis
  898. * readings defined for this sensor or 1 for scalar sensors.
  899. */
  900. r->readings_count = s->num_axis ?: 1;
  901. for (i = 0; i < r->readings_count; i++)
  902. scmi_parse_sensor_readings(&r->readings[i],
  903. &p->readings[i]);
  904. *src_id = r->sensor_id;
  905. rep = r;
  906. break;
  907. }
  908. default:
  909. break;
  910. }
  911. return rep;
  912. }
  913. static int scmi_sensor_get_num_sources(const struct scmi_protocol_handle *ph)
  914. {
  915. struct sensors_info *si = ph->get_priv(ph);
  916. return si->num_sensors;
  917. }
  918. static const struct scmi_event sensor_events[] = {
  919. {
  920. .id = SCMI_EVENT_SENSOR_TRIP_POINT_EVENT,
  921. .max_payld_sz = sizeof(struct scmi_sensor_trip_notify_payld),
  922. .max_report_sz = sizeof(struct scmi_sensor_trip_point_report),
  923. },
  924. {
  925. .id = SCMI_EVENT_SENSOR_UPDATE,
  926. .max_payld_sz =
  927. sizeof(struct scmi_sensor_update_notify_payld) +
  928. SCMI_MAX_NUM_SENSOR_AXIS *
  929. sizeof(struct scmi_sensor_reading_resp),
  930. .max_report_sz = sizeof(struct scmi_sensor_update_report) +
  931. SCMI_MAX_NUM_SENSOR_AXIS *
  932. sizeof(struct scmi_sensor_reading),
  933. },
  934. };
  935. static const struct scmi_event_ops sensor_event_ops = {
  936. .get_num_sources = scmi_sensor_get_num_sources,
  937. .set_notify_enabled = scmi_sensor_set_notify_enabled,
  938. .fill_custom_report = scmi_sensor_fill_custom_report,
  939. };
  940. static const struct scmi_protocol_events sensor_protocol_events = {
  941. .queue_sz = SCMI_PROTO_QUEUE_SZ,
  942. .ops = &sensor_event_ops,
  943. .evts = sensor_events,
  944. .num_events = ARRAY_SIZE(sensor_events),
  945. };
  946. static int scmi_sensors_protocol_init(const struct scmi_protocol_handle *ph)
  947. {
  948. u32 version;
  949. int ret;
  950. struct sensors_info *sinfo;
  951. ret = ph->xops->version_get(ph, &version);
  952. if (ret)
  953. return ret;
  954. dev_dbg(ph->dev, "Sensor Version %d.%d\n",
  955. PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
  956. sinfo = devm_kzalloc(ph->dev, sizeof(*sinfo), GFP_KERNEL);
  957. if (!sinfo)
  958. return -ENOMEM;
  959. sinfo->version = version;
  960. ret = scmi_sensor_attributes_get(ph, sinfo);
  961. if (ret)
  962. return ret;
  963. sinfo->sensors = devm_kcalloc(ph->dev, sinfo->num_sensors,
  964. sizeof(*sinfo->sensors), GFP_KERNEL);
  965. if (!sinfo->sensors)
  966. return -ENOMEM;
  967. ret = scmi_sensor_description_get(ph, sinfo);
  968. if (ret)
  969. return ret;
  970. return ph->set_priv(ph, sinfo);
  971. }
  972. static const struct scmi_protocol scmi_sensors = {
  973. .id = SCMI_PROTOCOL_SENSOR,
  974. .owner = THIS_MODULE,
  975. .instance_init = &scmi_sensors_protocol_init,
  976. .ops = &sensor_proto_ops,
  977. .events = &sensor_protocol_events,
  978. };
  979. DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(sensors, scmi_sensors)