pinctrl-nsp-mux.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2015 Broadcom Corporation
  3. *
  4. * This file contains the Northstar plus (NSP) IOMUX driver that supports
  5. * group based PINMUX configuration. The Northstar plus IOMUX controller
  6. * allows pins to be individually muxed to GPIO function. The NAND and MMC is
  7. * a group based selection. The gpio_a 8 - 11 are muxed with gpio_b and pwm.
  8. * To select PWM, one need to enable the corresponding gpio_b as well.
  9. *
  10. * gpio_a (8 - 11)
  11. * +----------
  12. * |
  13. * gpio_a (8-11) | gpio_b (0 - 3)
  14. * ------------------------+-------+----------
  15. * |
  16. * | pwm (0 - 3)
  17. * +----------
  18. */
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/of.h>
  22. #include <linux/pinctrl/pinconf.h>
  23. #include <linux/pinctrl/pinconf-generic.h>
  24. #include <linux/pinctrl/pinctrl.h>
  25. #include <linux/pinctrl/pinmux.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include "../core.h"
  29. #include "../pinctrl-utils.h"
  30. #define NSP_MUX_BASE0 0x00
  31. #define NSP_MUX_BASE1 0x01
  32. #define NSP_MUX_BASE2 0x02
  33. /*
  34. * nsp IOMUX register description
  35. *
  36. * @base: base 0 or base 1
  37. * @shift: bit shift for mux configuration of a group
  38. * @mask: bit mask of the function
  39. * @alt: alternate function to set to
  40. */
  41. struct nsp_mux {
  42. unsigned int base;
  43. unsigned int shift;
  44. unsigned int mask;
  45. unsigned int alt;
  46. };
  47. /*
  48. * Keep track of nsp IOMUX configuration and prevent double configuration
  49. *
  50. * @nsp_mux: nsp IOMUX register description
  51. * @is_configured: flag to indicate whether a mux setting has already been
  52. * configured
  53. */
  54. struct nsp_mux_log {
  55. struct nsp_mux mux;
  56. bool is_configured;
  57. };
  58. /*
  59. * Group based IOMUX configuration
  60. *
  61. * @name: name of the group
  62. * @pins: array of pins used by this group
  63. * @num_pins: total number of pins used by this group
  64. * @mux: nsp group based IOMUX configuration
  65. */
  66. struct nsp_pin_group {
  67. const char *name;
  68. const unsigned int *pins;
  69. const unsigned int num_pins;
  70. const struct nsp_mux mux;
  71. };
  72. /*
  73. * nsp mux function and supported pin groups
  74. *
  75. * @name: name of the function
  76. * @groups: array of groups that can be supported by this function
  77. * @num_groups: total number of groups that can be supported by this function
  78. */
  79. struct nsp_pin_function {
  80. const char *name;
  81. const char * const *groups;
  82. const unsigned int num_groups;
  83. };
  84. /*
  85. * nsp IOMUX pinctrl core
  86. *
  87. * @pctl: pointer to pinctrl_dev
  88. * @dev: pointer to device
  89. * @base0: first mux register
  90. * @base1: second mux register
  91. * @base2: third mux register
  92. * @groups: pointer to array of groups
  93. * @num_groups: total number of groups
  94. * @functions: pointer to array of functions
  95. * @num_functions: total number of functions
  96. * @mux_log: pointer to the array of mux logs
  97. * @lock: lock to protect register access
  98. */
  99. struct nsp_pinctrl {
  100. struct pinctrl_dev *pctl;
  101. struct device *dev;
  102. void __iomem *base0;
  103. void __iomem *base1;
  104. void __iomem *base2;
  105. const struct nsp_pin_group *groups;
  106. unsigned int num_groups;
  107. const struct nsp_pin_function *functions;
  108. unsigned int num_functions;
  109. struct nsp_mux_log *mux_log;
  110. spinlock_t lock;
  111. };
  112. /*
  113. * Description of a pin in nsp
  114. *
  115. * @pin: pin number
  116. * @name: pin name
  117. * @gpio_select: reg data to select GPIO
  118. */
  119. struct nsp_pin {
  120. unsigned int pin;
  121. char *name;
  122. unsigned int gpio_select;
  123. };
  124. #define NSP_PIN_DESC(p, n, g) \
  125. { \
  126. .pin = p, \
  127. .name = n, \
  128. .gpio_select = g, \
  129. }
  130. /*
  131. * List of muxable pins in nsp
  132. */
  133. static struct nsp_pin nsp_pins[] = {
  134. NSP_PIN_DESC(0, "spi_clk", 1),
  135. NSP_PIN_DESC(1, "spi_ss", 1),
  136. NSP_PIN_DESC(2, "spi_mosi", 1),
  137. NSP_PIN_DESC(3, "spi_miso", 1),
  138. NSP_PIN_DESC(4, "scl", 1),
  139. NSP_PIN_DESC(5, "sda", 1),
  140. NSP_PIN_DESC(6, "mdc", 1),
  141. NSP_PIN_DESC(7, "mdio", 1),
  142. NSP_PIN_DESC(8, "pwm0", 1),
  143. NSP_PIN_DESC(9, "pwm1", 1),
  144. NSP_PIN_DESC(10, "pwm2", 1),
  145. NSP_PIN_DESC(11, "pwm3", 1),
  146. NSP_PIN_DESC(12, "uart1_rx", 1),
  147. NSP_PIN_DESC(13, "uart1_tx", 1),
  148. NSP_PIN_DESC(14, "uart1_cts", 1),
  149. NSP_PIN_DESC(15, "uart1_rts", 1),
  150. NSP_PIN_DESC(16, "uart2_rx", 1),
  151. NSP_PIN_DESC(17, "uart2_tx", 1),
  152. NSP_PIN_DESC(18, "synce", 0),
  153. NSP_PIN_DESC(19, "sata0_led", 0),
  154. NSP_PIN_DESC(20, "sata1_led", 0),
  155. NSP_PIN_DESC(21, "xtal_out", 1),
  156. NSP_PIN_DESC(22, "sdio_pwr", 1),
  157. NSP_PIN_DESC(23, "sdio_en_1p8v", 1),
  158. NSP_PIN_DESC(24, "gpio_24", 1),
  159. NSP_PIN_DESC(25, "gpio_25", 1),
  160. NSP_PIN_DESC(26, "p5_led0", 0),
  161. NSP_PIN_DESC(27, "p5_led1", 0),
  162. NSP_PIN_DESC(28, "gpio_28", 1),
  163. NSP_PIN_DESC(29, "gpio_29", 1),
  164. NSP_PIN_DESC(30, "gpio_30", 1),
  165. NSP_PIN_DESC(31, "gpio_31", 1),
  166. NSP_PIN_DESC(32, "nand_ale", 0),
  167. NSP_PIN_DESC(33, "nand_ce0", 0),
  168. NSP_PIN_DESC(34, "nand_r/b", 0),
  169. NSP_PIN_DESC(35, "nand_dq0", 0),
  170. NSP_PIN_DESC(36, "nand_dq1", 0),
  171. NSP_PIN_DESC(37, "nand_dq2", 0),
  172. NSP_PIN_DESC(38, "nand_dq3", 0),
  173. NSP_PIN_DESC(39, "nand_dq4", 0),
  174. NSP_PIN_DESC(40, "nand_dq5", 0),
  175. NSP_PIN_DESC(41, "nand_dq6", 0),
  176. NSP_PIN_DESC(42, "nand_dq7", 0),
  177. };
  178. /*
  179. * List of groups of pins
  180. */
  181. static const unsigned int spi_pins[] = {0, 1, 2, 3};
  182. static const unsigned int i2c_pins[] = {4, 5};
  183. static const unsigned int mdio_pins[] = {6, 7};
  184. static const unsigned int pwm0_pins[] = {8};
  185. static const unsigned int gpio_b_0_pins[] = {8};
  186. static const unsigned int pwm1_pins[] = {9};
  187. static const unsigned int gpio_b_1_pins[] = {9};
  188. static const unsigned int pwm2_pins[] = {10};
  189. static const unsigned int gpio_b_2_pins[] = {10};
  190. static const unsigned int pwm3_pins[] = {11};
  191. static const unsigned int gpio_b_3_pins[] = {11};
  192. static const unsigned int uart1_pins[] = {12, 13, 14, 15};
  193. static const unsigned int uart2_pins[] = {16, 17};
  194. static const unsigned int synce_pins[] = {18};
  195. static const unsigned int sata0_led_pins[] = {19};
  196. static const unsigned int sata1_led_pins[] = {20};
  197. static const unsigned int xtal_out_pins[] = {21};
  198. static const unsigned int sdio_pwr_pins[] = {22};
  199. static const unsigned int sdio_1p8v_pins[] = {23};
  200. static const unsigned int switch_p05_led0_pins[] = {26};
  201. static const unsigned int switch_p05_led1_pins[] = {27};
  202. static const unsigned int nand_pins[] = {32, 33, 34, 35, 36, 37, 38, 39,
  203. 40, 41, 42};
  204. static const unsigned int emmc_pins[] = {32, 33, 34, 35, 36, 37, 38, 39,
  205. 40, 41, 42};
  206. #define NSP_PIN_GROUP(group_name, ba, sh, ma, al) \
  207. { \
  208. .name = __stringify(group_name) "_grp", \
  209. .pins = group_name ## _pins, \
  210. .num_pins = ARRAY_SIZE(group_name ## _pins), \
  211. .mux = { \
  212. .base = ba, \
  213. .shift = sh, \
  214. .mask = ma, \
  215. .alt = al, \
  216. } \
  217. }
  218. /*
  219. * List of nsp pin groups
  220. */
  221. static const struct nsp_pin_group nsp_pin_groups[] = {
  222. NSP_PIN_GROUP(spi, NSP_MUX_BASE0, 0, 0x0f, 0x00),
  223. NSP_PIN_GROUP(i2c, NSP_MUX_BASE0, 3, 0x03, 0x00),
  224. NSP_PIN_GROUP(mdio, NSP_MUX_BASE0, 5, 0x03, 0x00),
  225. NSP_PIN_GROUP(gpio_b_0, NSP_MUX_BASE0, 7, 0x01, 0x00),
  226. NSP_PIN_GROUP(pwm0, NSP_MUX_BASE1, 0, 0x01, 0x01),
  227. NSP_PIN_GROUP(gpio_b_1, NSP_MUX_BASE0, 8, 0x01, 0x00),
  228. NSP_PIN_GROUP(pwm1, NSP_MUX_BASE1, 1, 0x01, 0x01),
  229. NSP_PIN_GROUP(gpio_b_2, NSP_MUX_BASE0, 9, 0x01, 0x00),
  230. NSP_PIN_GROUP(pwm2, NSP_MUX_BASE1, 2, 0x01, 0x01),
  231. NSP_PIN_GROUP(gpio_b_3, NSP_MUX_BASE0, 10, 0x01, 0x00),
  232. NSP_PIN_GROUP(pwm3, NSP_MUX_BASE1, 3, 0x01, 0x01),
  233. NSP_PIN_GROUP(uart1, NSP_MUX_BASE0, 11, 0x0f, 0x00),
  234. NSP_PIN_GROUP(uart2, NSP_MUX_BASE0, 15, 0x03, 0x00),
  235. NSP_PIN_GROUP(synce, NSP_MUX_BASE0, 17, 0x01, 0x01),
  236. NSP_PIN_GROUP(sata0_led, NSP_MUX_BASE0, 18, 0x01, 0x01),
  237. NSP_PIN_GROUP(sata1_led, NSP_MUX_BASE0, 19, 0x01, 0x01),
  238. NSP_PIN_GROUP(xtal_out, NSP_MUX_BASE0, 20, 0x01, 0x00),
  239. NSP_PIN_GROUP(sdio_pwr, NSP_MUX_BASE0, 21, 0x01, 0x00),
  240. NSP_PIN_GROUP(sdio_1p8v, NSP_MUX_BASE0, 22, 0x01, 0x00),
  241. NSP_PIN_GROUP(switch_p05_led0, NSP_MUX_BASE0, 26, 0x01, 0x01),
  242. NSP_PIN_GROUP(switch_p05_led1, NSP_MUX_BASE0, 27, 0x01, 0x01),
  243. NSP_PIN_GROUP(nand, NSP_MUX_BASE2, 0, 0x01, 0x00),
  244. NSP_PIN_GROUP(emmc, NSP_MUX_BASE2, 0, 0x01, 0x01)
  245. };
  246. /*
  247. * List of groups supported by functions
  248. */
  249. static const char * const spi_grps[] = {"spi_grp"};
  250. static const char * const i2c_grps[] = {"i2c_grp"};
  251. static const char * const mdio_grps[] = {"mdio_grp"};
  252. static const char * const pwm_grps[] = {"pwm0_grp", "pwm1_grp", "pwm2_grp"
  253. , "pwm3_grp"};
  254. static const char * const gpio_b_grps[] = {"gpio_b_0_grp", "gpio_b_1_grp",
  255. "gpio_b_2_grp", "gpio_b_3_grp"};
  256. static const char * const uart1_grps[] = {"uart1_grp"};
  257. static const char * const uart2_grps[] = {"uart2_grp"};
  258. static const char * const synce_grps[] = {"synce_grp"};
  259. static const char * const sata_led_grps[] = {"sata0_led_grp", "sata1_led_grp"};
  260. static const char * const xtal_out_grps[] = {"xtal_out_grp"};
  261. static const char * const sdio_grps[] = {"sdio_pwr_grp", "sdio_1p8v_grp"};
  262. static const char * const switch_led_grps[] = {"switch_p05_led0_grp",
  263. "switch_p05_led1_grp"};
  264. static const char * const nand_grps[] = {"nand_grp"};
  265. static const char * const emmc_grps[] = {"emmc_grp"};
  266. #define NSP_PIN_FUNCTION(func) \
  267. { \
  268. .name = #func, \
  269. .groups = func ## _grps, \
  270. .num_groups = ARRAY_SIZE(func ## _grps), \
  271. }
  272. /*
  273. * List of supported functions in nsp
  274. */
  275. static const struct nsp_pin_function nsp_pin_functions[] = {
  276. NSP_PIN_FUNCTION(spi),
  277. NSP_PIN_FUNCTION(i2c),
  278. NSP_PIN_FUNCTION(mdio),
  279. NSP_PIN_FUNCTION(pwm),
  280. NSP_PIN_FUNCTION(gpio_b),
  281. NSP_PIN_FUNCTION(uart1),
  282. NSP_PIN_FUNCTION(uart2),
  283. NSP_PIN_FUNCTION(synce),
  284. NSP_PIN_FUNCTION(sata_led),
  285. NSP_PIN_FUNCTION(xtal_out),
  286. NSP_PIN_FUNCTION(sdio),
  287. NSP_PIN_FUNCTION(switch_led),
  288. NSP_PIN_FUNCTION(nand),
  289. NSP_PIN_FUNCTION(emmc)
  290. };
  291. static int nsp_get_groups_count(struct pinctrl_dev *pctrl_dev)
  292. {
  293. struct nsp_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  294. return pinctrl->num_groups;
  295. }
  296. static const char *nsp_get_group_name(struct pinctrl_dev *pctrl_dev,
  297. unsigned int selector)
  298. {
  299. struct nsp_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  300. return pinctrl->groups[selector].name;
  301. }
  302. static int nsp_get_group_pins(struct pinctrl_dev *pctrl_dev,
  303. unsigned int selector, const unsigned int **pins,
  304. unsigned int *num_pins)
  305. {
  306. struct nsp_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  307. *pins = pinctrl->groups[selector].pins;
  308. *num_pins = pinctrl->groups[selector].num_pins;
  309. return 0;
  310. }
  311. static void nsp_pin_dbg_show(struct pinctrl_dev *pctrl_dev,
  312. struct seq_file *s, unsigned int offset)
  313. {
  314. seq_printf(s, " %s", dev_name(pctrl_dev->dev));
  315. }
  316. static const struct pinctrl_ops nsp_pinctrl_ops = {
  317. .get_groups_count = nsp_get_groups_count,
  318. .get_group_name = nsp_get_group_name,
  319. .get_group_pins = nsp_get_group_pins,
  320. .pin_dbg_show = nsp_pin_dbg_show,
  321. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  322. .dt_free_map = pinctrl_utils_free_map,
  323. };
  324. static int nsp_get_functions_count(struct pinctrl_dev *pctrl_dev)
  325. {
  326. struct nsp_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  327. return pinctrl->num_functions;
  328. }
  329. static const char *nsp_get_function_name(struct pinctrl_dev *pctrl_dev,
  330. unsigned int selector)
  331. {
  332. struct nsp_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  333. return pinctrl->functions[selector].name;
  334. }
  335. static int nsp_get_function_groups(struct pinctrl_dev *pctrl_dev,
  336. unsigned int selector,
  337. const char * const **groups,
  338. unsigned * const num_groups)
  339. {
  340. struct nsp_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  341. *groups = pinctrl->functions[selector].groups;
  342. *num_groups = pinctrl->functions[selector].num_groups;
  343. return 0;
  344. }
  345. static int nsp_pinmux_set(struct nsp_pinctrl *pinctrl,
  346. const struct nsp_pin_function *func,
  347. const struct nsp_pin_group *grp,
  348. struct nsp_mux_log *mux_log)
  349. {
  350. const struct nsp_mux *mux = &grp->mux;
  351. int i;
  352. u32 val, mask;
  353. unsigned long flags;
  354. void __iomem *base_address;
  355. for (i = 0; i < pinctrl->num_groups; i++) {
  356. if ((mux->shift != mux_log[i].mux.shift) ||
  357. (mux->base != mux_log[i].mux.base))
  358. continue;
  359. /* if this is a new configuration, just do it! */
  360. if (!mux_log[i].is_configured)
  361. break;
  362. /*
  363. * IOMUX has been configured previously and one is trying to
  364. * configure it to a different function
  365. */
  366. if (mux_log[i].mux.alt != mux->alt) {
  367. dev_err(pinctrl->dev,
  368. "double configuration error detected!\n");
  369. dev_err(pinctrl->dev, "func:%s grp:%s\n",
  370. func->name, grp->name);
  371. return -EINVAL;
  372. }
  373. return 0;
  374. }
  375. if (i == pinctrl->num_groups)
  376. return -EINVAL;
  377. mask = mux->mask;
  378. mux_log[i].mux.alt = mux->alt;
  379. mux_log[i].is_configured = true;
  380. switch (mux->base) {
  381. case NSP_MUX_BASE0:
  382. base_address = pinctrl->base0;
  383. break;
  384. case NSP_MUX_BASE1:
  385. base_address = pinctrl->base1;
  386. break;
  387. case NSP_MUX_BASE2:
  388. base_address = pinctrl->base2;
  389. break;
  390. default:
  391. return -EINVAL;
  392. }
  393. spin_lock_irqsave(&pinctrl->lock, flags);
  394. val = readl(base_address);
  395. val &= ~(mask << grp->mux.shift);
  396. val |= grp->mux.alt << grp->mux.shift;
  397. writel(val, base_address);
  398. spin_unlock_irqrestore(&pinctrl->lock, flags);
  399. return 0;
  400. }
  401. static int nsp_pinmux_enable(struct pinctrl_dev *pctrl_dev,
  402. unsigned int func_select, unsigned int grp_select)
  403. {
  404. struct nsp_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  405. const struct nsp_pin_function *func;
  406. const struct nsp_pin_group *grp;
  407. if (grp_select >= pinctrl->num_groups ||
  408. func_select >= pinctrl->num_functions)
  409. return -EINVAL;
  410. func = &pinctrl->functions[func_select];
  411. grp = &pinctrl->groups[grp_select];
  412. dev_dbg(pctrl_dev->dev, "func:%u name:%s grp:%u name:%s\n",
  413. func_select, func->name, grp_select, grp->name);
  414. dev_dbg(pctrl_dev->dev, "shift:%u alt:%u\n", grp->mux.shift,
  415. grp->mux.alt);
  416. return nsp_pinmux_set(pinctrl, func, grp, pinctrl->mux_log);
  417. }
  418. static int nsp_gpio_request_enable(struct pinctrl_dev *pctrl_dev,
  419. struct pinctrl_gpio_range *range,
  420. unsigned int pin)
  421. {
  422. struct nsp_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  423. u32 *gpio_select = pctrl_dev->desc->pins[pin].drv_data;
  424. u32 val;
  425. unsigned long flags;
  426. spin_lock_irqsave(&pinctrl->lock, flags);
  427. val = readl(pinctrl->base0);
  428. if ((val & BIT(pin)) != (*gpio_select << pin)) {
  429. val &= ~BIT(pin);
  430. val |= *gpio_select << pin;
  431. writel(val, pinctrl->base0);
  432. }
  433. spin_unlock_irqrestore(&pinctrl->lock, flags);
  434. return 0;
  435. }
  436. static void nsp_gpio_disable_free(struct pinctrl_dev *pctrl_dev,
  437. struct pinctrl_gpio_range *range,
  438. unsigned int pin)
  439. {
  440. struct nsp_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  441. u32 *gpio_select = pctrl_dev->desc->pins[pin].drv_data;
  442. u32 val;
  443. unsigned long flags;
  444. spin_lock_irqsave(&pinctrl->lock, flags);
  445. val = readl(pinctrl->base0);
  446. if ((val & (1 << pin)) == (*gpio_select << pin)) {
  447. val &= ~(1 << pin);
  448. if (!(*gpio_select))
  449. val |= (1 << pin);
  450. writel(val, pinctrl->base0);
  451. }
  452. spin_unlock_irqrestore(&pinctrl->lock, flags);
  453. }
  454. static const struct pinmux_ops nsp_pinmux_ops = {
  455. .get_functions_count = nsp_get_functions_count,
  456. .get_function_name = nsp_get_function_name,
  457. .get_function_groups = nsp_get_function_groups,
  458. .set_mux = nsp_pinmux_enable,
  459. .gpio_request_enable = nsp_gpio_request_enable,
  460. .gpio_disable_free = nsp_gpio_disable_free,
  461. };
  462. static struct pinctrl_desc nsp_pinctrl_desc = {
  463. .name = "nsp-pinmux",
  464. .pctlops = &nsp_pinctrl_ops,
  465. .pmxops = &nsp_pinmux_ops,
  466. };
  467. static int nsp_mux_log_init(struct nsp_pinctrl *pinctrl)
  468. {
  469. struct nsp_mux_log *log;
  470. unsigned int i;
  471. u32 no_of_groups = ARRAY_SIZE(nsp_pin_groups);
  472. pinctrl->mux_log = devm_kcalloc(pinctrl->dev, no_of_groups,
  473. sizeof(struct nsp_mux_log),
  474. GFP_KERNEL);
  475. if (!pinctrl->mux_log)
  476. return -ENOMEM;
  477. for (i = 0; i < no_of_groups; i++) {
  478. log = &pinctrl->mux_log[i];
  479. log->mux.base = nsp_pin_groups[i].mux.base;
  480. log->mux.shift = nsp_pin_groups[i].mux.shift;
  481. log->mux.alt = 0;
  482. log->is_configured = false;
  483. }
  484. return 0;
  485. }
  486. static int nsp_pinmux_probe(struct platform_device *pdev)
  487. {
  488. struct nsp_pinctrl *pinctrl;
  489. struct resource *res;
  490. int i, ret;
  491. struct pinctrl_pin_desc *pins;
  492. unsigned int num_pins = ARRAY_SIZE(nsp_pins);
  493. pinctrl = devm_kzalloc(&pdev->dev, sizeof(*pinctrl), GFP_KERNEL);
  494. if (!pinctrl)
  495. return -ENOMEM;
  496. pinctrl->dev = &pdev->dev;
  497. platform_set_drvdata(pdev, pinctrl);
  498. spin_lock_init(&pinctrl->lock);
  499. pinctrl->base0 = devm_platform_ioremap_resource(pdev, 0);
  500. if (IS_ERR(pinctrl->base0))
  501. return PTR_ERR(pinctrl->base0);
  502. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  503. if (!res)
  504. return -EINVAL;
  505. pinctrl->base1 = devm_ioremap(&pdev->dev, res->start,
  506. resource_size(res));
  507. if (!pinctrl->base1) {
  508. dev_err(&pdev->dev, "unable to map I/O space\n");
  509. return -ENOMEM;
  510. }
  511. pinctrl->base2 = devm_platform_ioremap_resource(pdev, 2);
  512. if (IS_ERR(pinctrl->base2))
  513. return PTR_ERR(pinctrl->base2);
  514. ret = nsp_mux_log_init(pinctrl);
  515. if (ret) {
  516. dev_err(&pdev->dev, "unable to initialize IOMUX log\n");
  517. return ret;
  518. }
  519. pins = devm_kcalloc(&pdev->dev, num_pins, sizeof(*pins), GFP_KERNEL);
  520. if (!pins)
  521. return -ENOMEM;
  522. for (i = 0; i < num_pins; i++) {
  523. pins[i].number = nsp_pins[i].pin;
  524. pins[i].name = nsp_pins[i].name;
  525. pins[i].drv_data = &nsp_pins[i].gpio_select;
  526. }
  527. pinctrl->groups = nsp_pin_groups;
  528. pinctrl->num_groups = ARRAY_SIZE(nsp_pin_groups);
  529. pinctrl->functions = nsp_pin_functions;
  530. pinctrl->num_functions = ARRAY_SIZE(nsp_pin_functions);
  531. nsp_pinctrl_desc.pins = pins;
  532. nsp_pinctrl_desc.npins = num_pins;
  533. pinctrl->pctl = devm_pinctrl_register(&pdev->dev, &nsp_pinctrl_desc,
  534. pinctrl);
  535. if (IS_ERR(pinctrl->pctl)) {
  536. dev_err(&pdev->dev, "unable to register nsp IOMUX pinctrl\n");
  537. return PTR_ERR(pinctrl->pctl);
  538. }
  539. return 0;
  540. }
  541. static const struct of_device_id nsp_pinmux_of_match[] = {
  542. { .compatible = "brcm,nsp-pinmux" },
  543. { }
  544. };
  545. static struct platform_driver nsp_pinmux_driver = {
  546. .driver = {
  547. .name = "nsp-pinmux",
  548. .of_match_table = nsp_pinmux_of_match,
  549. },
  550. .probe = nsp_pinmux_probe,
  551. };
  552. static int __init nsp_pinmux_init(void)
  553. {
  554. return platform_driver_register(&nsp_pinmux_driver);
  555. }
  556. arch_initcall(nsp_pinmux_init);