pinctrl-nsp-mux.c 18 KB

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