pinctrl.c 21 KB

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