pinctrl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Pinctrl Protocol
  4. *
  5. * Copyright (C) 2024 EPAM
  6. * Copyright 2024 NXP
  7. */
  8. #include <asm/byteorder.h>
  9. #include <linux/bits.h>
  10. #include <linux/bitfield.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/scmi_protocol.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include "common.h"
  18. #include "protocols.h"
  19. /* Updated only after ALL the mandatory features for that version are merged */
  20. #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x10000
  21. #define GET_GROUPS_NR(x) le32_get_bits((x), GENMASK(31, 16))
  22. #define GET_PINS_NR(x) le32_get_bits((x), GENMASK(15, 0))
  23. #define GET_FUNCTIONS_NR(x) le32_get_bits((x), GENMASK(15, 0))
  24. #define EXT_NAME_FLAG(x) le32_get_bits((x), BIT(31))
  25. #define NUM_ELEMS(x) le32_get_bits((x), GENMASK(15, 0))
  26. #define REMAINING(x) le32_get_bits((x), GENMASK(31, 16))
  27. #define RETURNED(x) le32_get_bits((x), GENMASK(11, 0))
  28. #define CONFIG_FLAG_MASK GENMASK(19, 18)
  29. #define SELECTOR_MASK GENMASK(17, 16)
  30. #define SKIP_CONFIGS_MASK GENMASK(15, 8)
  31. #define CONFIG_TYPE_MASK GENMASK(7, 0)
  32. enum scmi_pinctrl_protocol_cmd {
  33. PINCTRL_ATTRIBUTES = 0x3,
  34. PINCTRL_LIST_ASSOCIATIONS = 0x4,
  35. PINCTRL_SETTINGS_GET = 0x5,
  36. PINCTRL_SETTINGS_CONFIGURE = 0x6,
  37. PINCTRL_REQUEST = 0x7,
  38. PINCTRL_RELEASE = 0x8,
  39. PINCTRL_NAME_GET = 0x9,
  40. PINCTRL_SET_PERMISSIONS = 0xa,
  41. };
  42. struct scmi_msg_settings_conf {
  43. __le32 identifier;
  44. __le32 function_id;
  45. __le32 attributes;
  46. __le32 configs[];
  47. };
  48. struct scmi_msg_settings_get {
  49. __le32 identifier;
  50. __le32 attributes;
  51. };
  52. struct scmi_resp_settings_get {
  53. __le32 function_selected;
  54. __le32 num_configs;
  55. __le32 configs[];
  56. };
  57. struct scmi_msg_pinctrl_protocol_attributes {
  58. __le32 attributes_low;
  59. __le32 attributes_high;
  60. };
  61. struct scmi_msg_pinctrl_attributes {
  62. __le32 identifier;
  63. __le32 flags;
  64. };
  65. struct scmi_resp_pinctrl_attributes {
  66. __le32 attributes;
  67. u8 name[SCMI_SHORT_NAME_MAX_SIZE];
  68. };
  69. struct scmi_msg_pinctrl_list_assoc {
  70. __le32 identifier;
  71. __le32 flags;
  72. __le32 index;
  73. };
  74. struct scmi_resp_pinctrl_list_assoc {
  75. __le32 flags;
  76. __le16 array[];
  77. };
  78. struct scmi_msg_request {
  79. __le32 identifier;
  80. __le32 flags;
  81. };
  82. struct scmi_group_info {
  83. char name[SCMI_MAX_STR_SIZE];
  84. bool present;
  85. u32 *group_pins;
  86. u32 nr_pins;
  87. };
  88. struct scmi_function_info {
  89. char name[SCMI_MAX_STR_SIZE];
  90. bool present;
  91. u32 *groups;
  92. u32 nr_groups;
  93. };
  94. struct scmi_pin_info {
  95. char name[SCMI_MAX_STR_SIZE];
  96. bool present;
  97. };
  98. struct scmi_pinctrl_info {
  99. u32 version;
  100. int nr_groups;
  101. int nr_functions;
  102. int nr_pins;
  103. struct scmi_group_info *groups;
  104. struct scmi_function_info *functions;
  105. struct scmi_pin_info *pins;
  106. };
  107. static int scmi_pinctrl_attributes_get(const struct scmi_protocol_handle *ph,
  108. struct scmi_pinctrl_info *pi)
  109. {
  110. int ret;
  111. struct scmi_xfer *t;
  112. struct scmi_msg_pinctrl_protocol_attributes *attr;
  113. ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0, sizeof(*attr), &t);
  114. if (ret)
  115. return ret;
  116. attr = t->rx.buf;
  117. ret = ph->xops->do_xfer(ph, t);
  118. if (!ret) {
  119. pi->nr_functions = GET_FUNCTIONS_NR(attr->attributes_high);
  120. pi->nr_groups = GET_GROUPS_NR(attr->attributes_low);
  121. pi->nr_pins = GET_PINS_NR(attr->attributes_low);
  122. if (pi->nr_pins == 0) {
  123. dev_warn(ph->dev, "returned zero pins\n");
  124. ret = -EINVAL;
  125. }
  126. }
  127. ph->xops->xfer_put(ph, t);
  128. return ret;
  129. }
  130. static int scmi_pinctrl_count_get(const struct scmi_protocol_handle *ph,
  131. enum scmi_pinctrl_selector_type type)
  132. {
  133. struct scmi_pinctrl_info *pi = ph->get_priv(ph);
  134. switch (type) {
  135. case PIN_TYPE:
  136. return pi->nr_pins;
  137. case GROUP_TYPE:
  138. return pi->nr_groups;
  139. case FUNCTION_TYPE:
  140. return pi->nr_functions;
  141. default:
  142. return -EINVAL;
  143. }
  144. }
  145. static int scmi_pinctrl_validate_id(const struct scmi_protocol_handle *ph,
  146. u32 selector,
  147. enum scmi_pinctrl_selector_type type)
  148. {
  149. int value;
  150. value = scmi_pinctrl_count_get(ph, type);
  151. if (value < 0)
  152. return value;
  153. if (selector >= value || value == 0)
  154. return -EINVAL;
  155. return 0;
  156. }
  157. static int scmi_pinctrl_attributes(const struct scmi_protocol_handle *ph,
  158. enum scmi_pinctrl_selector_type type,
  159. u32 selector, char *name,
  160. u32 *n_elems)
  161. {
  162. int ret;
  163. struct scmi_xfer *t;
  164. struct scmi_msg_pinctrl_attributes *tx;
  165. struct scmi_resp_pinctrl_attributes *rx;
  166. bool ext_name_flag;
  167. if (!name)
  168. return -EINVAL;
  169. ret = scmi_pinctrl_validate_id(ph, selector, type);
  170. if (ret)
  171. return ret;
  172. ret = ph->xops->xfer_get_init(ph, PINCTRL_ATTRIBUTES, sizeof(*tx),
  173. sizeof(*rx), &t);
  174. if (ret)
  175. return ret;
  176. tx = t->tx.buf;
  177. rx = t->rx.buf;
  178. tx->identifier = cpu_to_le32(selector);
  179. tx->flags = cpu_to_le32(type);
  180. ret = ph->xops->do_xfer(ph, t);
  181. if (!ret) {
  182. if (n_elems)
  183. *n_elems = NUM_ELEMS(rx->attributes);
  184. strscpy(name, rx->name, SCMI_SHORT_NAME_MAX_SIZE);
  185. ext_name_flag = !!EXT_NAME_FLAG(rx->attributes);
  186. }
  187. ph->xops->xfer_put(ph, t);
  188. if (ret)
  189. return ret;
  190. /*
  191. * If supported overwrite short name with the extended one;
  192. * on error just carry on and use already provided short name.
  193. */
  194. if (ext_name_flag)
  195. ret = ph->hops->extended_name_get(ph, PINCTRL_NAME_GET,
  196. selector, (u32 *)&type, name,
  197. SCMI_MAX_STR_SIZE);
  198. return ret;
  199. }
  200. struct scmi_pinctrl_ipriv {
  201. u32 selector;
  202. enum scmi_pinctrl_selector_type type;
  203. u32 *array;
  204. };
  205. static void iter_pinctrl_assoc_prepare_message(void *message,
  206. u32 desc_index,
  207. const void *priv)
  208. {
  209. struct scmi_msg_pinctrl_list_assoc *msg = message;
  210. const struct scmi_pinctrl_ipriv *p = priv;
  211. msg->identifier = cpu_to_le32(p->selector);
  212. msg->flags = cpu_to_le32(p->type);
  213. msg->index = cpu_to_le32(desc_index);
  214. }
  215. static int iter_pinctrl_assoc_update_state(struct scmi_iterator_state *st,
  216. const void *response, void *priv)
  217. {
  218. const struct scmi_resp_pinctrl_list_assoc *r = response;
  219. st->num_returned = RETURNED(r->flags);
  220. st->num_remaining = REMAINING(r->flags);
  221. return 0;
  222. }
  223. static int
  224. iter_pinctrl_assoc_process_response(const struct scmi_protocol_handle *ph,
  225. const void *response,
  226. struct scmi_iterator_state *st, void *priv)
  227. {
  228. const struct scmi_resp_pinctrl_list_assoc *r = response;
  229. struct scmi_pinctrl_ipriv *p = priv;
  230. p->array[st->desc_index + st->loop_idx] =
  231. le16_to_cpu(r->array[st->loop_idx]);
  232. return 0;
  233. }
  234. static int scmi_pinctrl_list_associations(const struct scmi_protocol_handle *ph,
  235. u32 selector,
  236. enum scmi_pinctrl_selector_type type,
  237. u16 size, u32 *array)
  238. {
  239. int ret;
  240. void *iter;
  241. struct scmi_iterator_ops ops = {
  242. .prepare_message = iter_pinctrl_assoc_prepare_message,
  243. .update_state = iter_pinctrl_assoc_update_state,
  244. .process_response = iter_pinctrl_assoc_process_response,
  245. };
  246. struct scmi_pinctrl_ipriv ipriv = {
  247. .selector = selector,
  248. .type = type,
  249. .array = array,
  250. };
  251. if (!array || !size || type == PIN_TYPE)
  252. return -EINVAL;
  253. ret = scmi_pinctrl_validate_id(ph, selector, type);
  254. if (ret)
  255. return ret;
  256. iter = ph->hops->iter_response_init(ph, &ops, size,
  257. PINCTRL_LIST_ASSOCIATIONS,
  258. sizeof(struct scmi_msg_pinctrl_list_assoc),
  259. &ipriv);
  260. if (IS_ERR(iter))
  261. return PTR_ERR(iter);
  262. return ph->hops->iter_response_run(iter);
  263. }
  264. struct scmi_settings_get_ipriv {
  265. u32 selector;
  266. enum scmi_pinctrl_selector_type type;
  267. bool get_all;
  268. unsigned int *nr_configs;
  269. enum scmi_pinctrl_conf_type *config_types;
  270. u32 *config_values;
  271. };
  272. static void
  273. iter_pinctrl_settings_get_prepare_message(void *message, u32 desc_index,
  274. const void *priv)
  275. {
  276. struct scmi_msg_settings_get *msg = message;
  277. const struct scmi_settings_get_ipriv *p = priv;
  278. u32 attributes;
  279. attributes = FIELD_PREP(SELECTOR_MASK, p->type);
  280. if (p->get_all) {
  281. attributes |= FIELD_PREP(CONFIG_FLAG_MASK, 1) |
  282. FIELD_PREP(SKIP_CONFIGS_MASK, desc_index);
  283. } else {
  284. attributes |= FIELD_PREP(CONFIG_TYPE_MASK, p->config_types[0]);
  285. }
  286. msg->attributes = cpu_to_le32(attributes);
  287. msg->identifier = cpu_to_le32(p->selector);
  288. }
  289. static int
  290. iter_pinctrl_settings_get_update_state(struct scmi_iterator_state *st,
  291. const void *response, void *priv)
  292. {
  293. const struct scmi_resp_settings_get *r = response;
  294. struct scmi_settings_get_ipriv *p = priv;
  295. if (p->get_all) {
  296. st->num_returned = le32_get_bits(r->num_configs, GENMASK(7, 0));
  297. st->num_remaining = le32_get_bits(r->num_configs, GENMASK(31, 24));
  298. } else {
  299. st->num_returned = 1;
  300. st->num_remaining = 0;
  301. }
  302. return 0;
  303. }
  304. static int
  305. iter_pinctrl_settings_get_process_response(const struct scmi_protocol_handle *ph,
  306. const void *response,
  307. struct scmi_iterator_state *st,
  308. void *priv)
  309. {
  310. const struct scmi_resp_settings_get *r = response;
  311. struct scmi_settings_get_ipriv *p = priv;
  312. u32 type = le32_get_bits(r->configs[st->loop_idx * 2], GENMASK(7, 0));
  313. u32 val = le32_to_cpu(r->configs[st->loop_idx * 2 + 1]);
  314. if (p->get_all) {
  315. p->config_types[st->desc_index + st->loop_idx] = type;
  316. } else {
  317. if (p->config_types[0] != type)
  318. return -EINVAL;
  319. }
  320. p->config_values[st->desc_index + st->loop_idx] = val;
  321. ++*p->nr_configs;
  322. return 0;
  323. }
  324. static int
  325. scmi_pinctrl_settings_get(const struct scmi_protocol_handle *ph, u32 selector,
  326. enum scmi_pinctrl_selector_type type,
  327. unsigned int *nr_configs,
  328. enum scmi_pinctrl_conf_type *config_types,
  329. u32 *config_values)
  330. {
  331. int ret;
  332. void *iter;
  333. unsigned int max_configs = *nr_configs;
  334. struct scmi_iterator_ops ops = {
  335. .prepare_message = iter_pinctrl_settings_get_prepare_message,
  336. .update_state = iter_pinctrl_settings_get_update_state,
  337. .process_response = iter_pinctrl_settings_get_process_response,
  338. };
  339. struct scmi_settings_get_ipriv ipriv = {
  340. .selector = selector,
  341. .type = type,
  342. .get_all = (max_configs > 1),
  343. .nr_configs = nr_configs,
  344. .config_types = config_types,
  345. .config_values = config_values,
  346. };
  347. if (!config_types || !config_values || type == FUNCTION_TYPE)
  348. return -EINVAL;
  349. ret = scmi_pinctrl_validate_id(ph, selector, type);
  350. if (ret)
  351. return ret;
  352. /* Prepare to count returned configs */
  353. *nr_configs = 0;
  354. iter = ph->hops->iter_response_init(ph, &ops, max_configs,
  355. PINCTRL_SETTINGS_GET,
  356. sizeof(struct scmi_msg_settings_get),
  357. &ipriv);
  358. if (IS_ERR(iter))
  359. return PTR_ERR(iter);
  360. return ph->hops->iter_response_run(iter);
  361. }
  362. static int scmi_pinctrl_settings_get_one(const struct scmi_protocol_handle *ph,
  363. u32 selector,
  364. enum scmi_pinctrl_selector_type type,
  365. enum scmi_pinctrl_conf_type config_type,
  366. u32 *config_value)
  367. {
  368. unsigned int nr_configs = 1;
  369. return scmi_pinctrl_settings_get(ph, selector, type, &nr_configs,
  370. &config_type, config_value);
  371. }
  372. static int scmi_pinctrl_settings_get_all(const struct scmi_protocol_handle *ph,
  373. u32 selector,
  374. enum scmi_pinctrl_selector_type type,
  375. unsigned int *nr_configs,
  376. enum scmi_pinctrl_conf_type *config_types,
  377. u32 *config_values)
  378. {
  379. if (!nr_configs || *nr_configs == 0)
  380. return -EINVAL;
  381. return scmi_pinctrl_settings_get(ph, selector, type, nr_configs,
  382. config_types, config_values);
  383. }
  384. static int
  385. scmi_pinctrl_settings_conf(const struct scmi_protocol_handle *ph,
  386. u32 selector,
  387. enum scmi_pinctrl_selector_type type,
  388. u32 nr_configs,
  389. enum scmi_pinctrl_conf_type *config_type,
  390. u32 *config_value)
  391. {
  392. struct scmi_xfer *t;
  393. struct scmi_msg_settings_conf *tx;
  394. u32 attributes;
  395. int ret, i;
  396. u32 configs_in_chunk, conf_num = 0;
  397. u32 chunk;
  398. int max_msg_size = ph->hops->get_max_msg_size(ph);
  399. if (!config_type || !config_value || type == FUNCTION_TYPE)
  400. return -EINVAL;
  401. ret = scmi_pinctrl_validate_id(ph, selector, type);
  402. if (ret)
  403. return ret;
  404. configs_in_chunk = (max_msg_size - sizeof(*tx)) / (sizeof(__le32) * 2);
  405. while (conf_num < nr_configs) {
  406. chunk = (nr_configs - conf_num > configs_in_chunk) ?
  407. configs_in_chunk : nr_configs - conf_num;
  408. ret = ph->xops->xfer_get_init(ph, PINCTRL_SETTINGS_CONFIGURE,
  409. sizeof(*tx) +
  410. chunk * 2 * sizeof(__le32), 0, &t);
  411. if (ret)
  412. break;
  413. tx = t->tx.buf;
  414. tx->identifier = cpu_to_le32(selector);
  415. tx->function_id = cpu_to_le32(0xFFFFFFFF);
  416. attributes = FIELD_PREP(GENMASK(1, 0), type) |
  417. FIELD_PREP(GENMASK(9, 2), chunk);
  418. tx->attributes = cpu_to_le32(attributes);
  419. for (i = 0; i < chunk; i++) {
  420. tx->configs[i * 2] =
  421. cpu_to_le32(config_type[conf_num + i]);
  422. tx->configs[i * 2 + 1] =
  423. cpu_to_le32(config_value[conf_num + i]);
  424. }
  425. ret = ph->xops->do_xfer(ph, t);
  426. ph->xops->xfer_put(ph, t);
  427. if (ret)
  428. break;
  429. conf_num += chunk;
  430. }
  431. return ret;
  432. }
  433. static int scmi_pinctrl_function_select(const struct scmi_protocol_handle *ph,
  434. u32 group,
  435. enum scmi_pinctrl_selector_type type,
  436. u32 function_id)
  437. {
  438. int ret;
  439. struct scmi_xfer *t;
  440. struct scmi_msg_settings_conf *tx;
  441. u32 attributes;
  442. ret = scmi_pinctrl_validate_id(ph, group, type);
  443. if (ret)
  444. return ret;
  445. ret = ph->xops->xfer_get_init(ph, PINCTRL_SETTINGS_CONFIGURE,
  446. sizeof(*tx), 0, &t);
  447. if (ret)
  448. return ret;
  449. tx = t->tx.buf;
  450. tx->identifier = cpu_to_le32(group);
  451. tx->function_id = cpu_to_le32(function_id);
  452. attributes = FIELD_PREP(GENMASK(1, 0), type) | BIT(10);
  453. tx->attributes = cpu_to_le32(attributes);
  454. ret = ph->xops->do_xfer(ph, t);
  455. ph->xops->xfer_put(ph, t);
  456. return ret;
  457. }
  458. static int scmi_pinctrl_request_free(const struct scmi_protocol_handle *ph,
  459. u32 identifier,
  460. enum scmi_pinctrl_selector_type type,
  461. enum scmi_pinctrl_protocol_cmd cmd)
  462. {
  463. int ret;
  464. struct scmi_xfer *t;
  465. struct scmi_msg_request *tx;
  466. if (type == FUNCTION_TYPE)
  467. return -EINVAL;
  468. if (cmd != PINCTRL_REQUEST && cmd != PINCTRL_RELEASE)
  469. return -EINVAL;
  470. ret = scmi_pinctrl_validate_id(ph, identifier, type);
  471. if (ret)
  472. return ret;
  473. ret = ph->xops->xfer_get_init(ph, cmd, sizeof(*tx), 0, &t);
  474. if (ret)
  475. return ret;
  476. tx = t->tx.buf;
  477. tx->identifier = cpu_to_le32(identifier);
  478. tx->flags = cpu_to_le32(type);
  479. ret = ph->xops->do_xfer(ph, t);
  480. ph->xops->xfer_put(ph, t);
  481. return ret;
  482. }
  483. static int scmi_pinctrl_pin_request(const struct scmi_protocol_handle *ph,
  484. u32 pin)
  485. {
  486. return scmi_pinctrl_request_free(ph, pin, PIN_TYPE, PINCTRL_REQUEST);
  487. }
  488. static int scmi_pinctrl_pin_free(const struct scmi_protocol_handle *ph, u32 pin)
  489. {
  490. return scmi_pinctrl_request_free(ph, pin, PIN_TYPE, PINCTRL_RELEASE);
  491. }
  492. static int scmi_pinctrl_get_group_info(const struct scmi_protocol_handle *ph,
  493. u32 selector,
  494. struct scmi_group_info *group)
  495. {
  496. int ret;
  497. ret = scmi_pinctrl_attributes(ph, GROUP_TYPE, selector, group->name,
  498. &group->nr_pins);
  499. if (ret)
  500. return ret;
  501. if (!group->nr_pins) {
  502. dev_err(ph->dev, "Group %d has 0 elements", selector);
  503. return -ENODATA;
  504. }
  505. group->group_pins = kmalloc_array(group->nr_pins,
  506. sizeof(*group->group_pins),
  507. GFP_KERNEL);
  508. if (!group->group_pins)
  509. return -ENOMEM;
  510. ret = scmi_pinctrl_list_associations(ph, selector, GROUP_TYPE,
  511. group->nr_pins, group->group_pins);
  512. if (ret) {
  513. kfree(group->group_pins);
  514. return ret;
  515. }
  516. group->present = true;
  517. return 0;
  518. }
  519. static int scmi_pinctrl_get_group_name(const struct scmi_protocol_handle *ph,
  520. u32 selector, const char **name)
  521. {
  522. struct scmi_pinctrl_info *pi = ph->get_priv(ph);
  523. if (!name)
  524. return -EINVAL;
  525. if (selector >= pi->nr_groups || pi->nr_groups == 0)
  526. return -EINVAL;
  527. if (!pi->groups[selector].present) {
  528. int ret;
  529. ret = scmi_pinctrl_get_group_info(ph, selector,
  530. &pi->groups[selector]);
  531. if (ret)
  532. return ret;
  533. }
  534. *name = pi->groups[selector].name;
  535. return 0;
  536. }
  537. static int scmi_pinctrl_group_pins_get(const struct scmi_protocol_handle *ph,
  538. u32 selector, const u32 **pins,
  539. u32 *nr_pins)
  540. {
  541. struct scmi_pinctrl_info *pi = ph->get_priv(ph);
  542. if (!pins || !nr_pins)
  543. return -EINVAL;
  544. if (selector >= pi->nr_groups || pi->nr_groups == 0)
  545. return -EINVAL;
  546. if (!pi->groups[selector].present) {
  547. int ret;
  548. ret = scmi_pinctrl_get_group_info(ph, selector,
  549. &pi->groups[selector]);
  550. if (ret)
  551. return ret;
  552. }
  553. *pins = pi->groups[selector].group_pins;
  554. *nr_pins = pi->groups[selector].nr_pins;
  555. return 0;
  556. }
  557. static int scmi_pinctrl_get_function_info(const struct scmi_protocol_handle *ph,
  558. u32 selector,
  559. struct scmi_function_info *func)
  560. {
  561. int ret;
  562. ret = scmi_pinctrl_attributes(ph, FUNCTION_TYPE, selector, func->name,
  563. &func->nr_groups);
  564. if (ret)
  565. return ret;
  566. if (!func->nr_groups) {
  567. dev_err(ph->dev, "Function %d has 0 elements", selector);
  568. return -ENODATA;
  569. }
  570. func->groups = kmalloc_array(func->nr_groups, sizeof(*func->groups),
  571. GFP_KERNEL);
  572. if (!func->groups)
  573. return -ENOMEM;
  574. ret = scmi_pinctrl_list_associations(ph, selector, FUNCTION_TYPE,
  575. func->nr_groups, func->groups);
  576. if (ret) {
  577. kfree(func->groups);
  578. return ret;
  579. }
  580. func->present = true;
  581. return 0;
  582. }
  583. static int scmi_pinctrl_get_function_name(const struct scmi_protocol_handle *ph,
  584. u32 selector, const char **name)
  585. {
  586. struct scmi_pinctrl_info *pi = ph->get_priv(ph);
  587. if (!name)
  588. return -EINVAL;
  589. if (selector >= pi->nr_functions || pi->nr_functions == 0)
  590. return -EINVAL;
  591. if (!pi->functions[selector].present) {
  592. int ret;
  593. ret = scmi_pinctrl_get_function_info(ph, selector,
  594. &pi->functions[selector]);
  595. if (ret)
  596. return ret;
  597. }
  598. *name = pi->functions[selector].name;
  599. return 0;
  600. }
  601. static int
  602. scmi_pinctrl_function_groups_get(const struct scmi_protocol_handle *ph,
  603. u32 selector, u32 *nr_groups,
  604. const u32 **groups)
  605. {
  606. struct scmi_pinctrl_info *pi = ph->get_priv(ph);
  607. if (!groups || !nr_groups)
  608. return -EINVAL;
  609. if (selector >= pi->nr_functions || pi->nr_functions == 0)
  610. return -EINVAL;
  611. if (!pi->functions[selector].present) {
  612. int ret;
  613. ret = scmi_pinctrl_get_function_info(ph, selector,
  614. &pi->functions[selector]);
  615. if (ret)
  616. return ret;
  617. }
  618. *groups = pi->functions[selector].groups;
  619. *nr_groups = pi->functions[selector].nr_groups;
  620. return 0;
  621. }
  622. static int scmi_pinctrl_mux_set(const struct scmi_protocol_handle *ph,
  623. u32 selector, u32 group)
  624. {
  625. return scmi_pinctrl_function_select(ph, group, GROUP_TYPE, selector);
  626. }
  627. static int scmi_pinctrl_get_pin_info(const struct scmi_protocol_handle *ph,
  628. u32 selector, struct scmi_pin_info *pin)
  629. {
  630. int ret;
  631. if (!pin)
  632. return -EINVAL;
  633. ret = scmi_pinctrl_attributes(ph, PIN_TYPE, selector, pin->name, NULL);
  634. if (ret)
  635. return ret;
  636. pin->present = true;
  637. return 0;
  638. }
  639. static int scmi_pinctrl_get_pin_name(const struct scmi_protocol_handle *ph,
  640. u32 selector, const char **name)
  641. {
  642. struct scmi_pinctrl_info *pi = ph->get_priv(ph);
  643. if (!name)
  644. return -EINVAL;
  645. if (selector >= pi->nr_pins)
  646. return -EINVAL;
  647. if (!pi->pins[selector].present) {
  648. int ret;
  649. ret = scmi_pinctrl_get_pin_info(ph, selector, &pi->pins[selector]);
  650. if (ret)
  651. return ret;
  652. }
  653. *name = pi->pins[selector].name;
  654. return 0;
  655. }
  656. static int scmi_pinctrl_name_get(const struct scmi_protocol_handle *ph,
  657. u32 selector,
  658. enum scmi_pinctrl_selector_type type,
  659. const char **name)
  660. {
  661. switch (type) {
  662. case PIN_TYPE:
  663. return scmi_pinctrl_get_pin_name(ph, selector, name);
  664. case GROUP_TYPE:
  665. return scmi_pinctrl_get_group_name(ph, selector, name);
  666. case FUNCTION_TYPE:
  667. return scmi_pinctrl_get_function_name(ph, selector, name);
  668. default:
  669. return -EINVAL;
  670. }
  671. }
  672. static const struct scmi_pinctrl_proto_ops pinctrl_proto_ops = {
  673. .count_get = scmi_pinctrl_count_get,
  674. .name_get = scmi_pinctrl_name_get,
  675. .group_pins_get = scmi_pinctrl_group_pins_get,
  676. .function_groups_get = scmi_pinctrl_function_groups_get,
  677. .mux_set = scmi_pinctrl_mux_set,
  678. .settings_get_one = scmi_pinctrl_settings_get_one,
  679. .settings_get_all = scmi_pinctrl_settings_get_all,
  680. .settings_conf = scmi_pinctrl_settings_conf,
  681. .pin_request = scmi_pinctrl_pin_request,
  682. .pin_free = scmi_pinctrl_pin_free,
  683. };
  684. static int scmi_pinctrl_protocol_init(const struct scmi_protocol_handle *ph)
  685. {
  686. int ret;
  687. u32 version;
  688. struct scmi_pinctrl_info *pinfo;
  689. ret = ph->xops->version_get(ph, &version);
  690. if (ret)
  691. return ret;
  692. dev_dbg(ph->dev, "Pinctrl Version %d.%d\n",
  693. PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
  694. pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
  695. if (!pinfo)
  696. return -ENOMEM;
  697. ret = scmi_pinctrl_attributes_get(ph, pinfo);
  698. if (ret)
  699. return ret;
  700. pinfo->pins = devm_kcalloc(ph->dev, pinfo->nr_pins,
  701. sizeof(*pinfo->pins), GFP_KERNEL);
  702. if (!pinfo->pins)
  703. return -ENOMEM;
  704. pinfo->groups = devm_kcalloc(ph->dev, pinfo->nr_groups,
  705. sizeof(*pinfo->groups), GFP_KERNEL);
  706. if (!pinfo->groups)
  707. return -ENOMEM;
  708. pinfo->functions = devm_kcalloc(ph->dev, pinfo->nr_functions,
  709. sizeof(*pinfo->functions), GFP_KERNEL);
  710. if (!pinfo->functions)
  711. return -ENOMEM;
  712. pinfo->version = version;
  713. return ph->set_priv(ph, pinfo, version);
  714. }
  715. static int scmi_pinctrl_protocol_deinit(const struct scmi_protocol_handle *ph)
  716. {
  717. int i;
  718. struct scmi_pinctrl_info *pi = ph->get_priv(ph);
  719. /* Free groups_pins allocated in scmi_pinctrl_get_group_info */
  720. for (i = 0; i < pi->nr_groups; i++) {
  721. if (pi->groups[i].present) {
  722. kfree(pi->groups[i].group_pins);
  723. pi->groups[i].present = false;
  724. }
  725. }
  726. /* Free groups allocated in scmi_pinctrl_get_function_info */
  727. for (i = 0; i < pi->nr_functions; i++) {
  728. if (pi->functions[i].present) {
  729. kfree(pi->functions[i].groups);
  730. pi->functions[i].present = false;
  731. }
  732. }
  733. return 0;
  734. }
  735. static const struct scmi_protocol scmi_pinctrl = {
  736. .id = SCMI_PROTOCOL_PINCTRL,
  737. .owner = THIS_MODULE,
  738. .instance_init = &scmi_pinctrl_protocol_init,
  739. .instance_deinit = &scmi_pinctrl_protocol_deinit,
  740. .ops = &pinctrl_proto_ops,
  741. .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,
  742. };
  743. DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(pinctrl, scmi_pinctrl)