pinctrl-single.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /*
  2. * Generic device tree based pinctrl driver for one register per pin
  3. * type pinmux controllers
  4. *
  5. * Copyright (C) 2012 Texas Instruments, Inc.
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/io.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/list.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_address.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include "core.h"
  23. #define DRIVER_NAME "pinctrl-single"
  24. #define PCS_MUX_PINS_NAME "pinctrl-single,pins"
  25. #define PCS_MUX_BITS_NAME "pinctrl-single,bits"
  26. #define PCS_REG_NAME_LEN ((sizeof(unsigned long) * 2) + 1)
  27. #define PCS_OFF_DISABLED ~0U
  28. #define PCS_MAX_GPIO_VALUES 2
  29. /**
  30. * struct pcs_pingroup - pingroups for a function
  31. * @np: pingroup device node pointer
  32. * @name: pingroup name
  33. * @gpins: array of the pins in the group
  34. * @ngpins: number of pins in the group
  35. * @node: list node
  36. */
  37. struct pcs_pingroup {
  38. struct device_node *np;
  39. const char *name;
  40. int *gpins;
  41. int ngpins;
  42. struct list_head node;
  43. };
  44. /**
  45. * struct pcs_func_vals - mux function register offset and value pair
  46. * @reg: register virtual address
  47. * @val: register value
  48. */
  49. struct pcs_func_vals {
  50. void __iomem *reg;
  51. unsigned val;
  52. unsigned mask;
  53. };
  54. /**
  55. * struct pcs_function - pinctrl function
  56. * @name: pinctrl function name
  57. * @vals: register and vals array
  58. * @nvals: number of entries in vals array
  59. * @pgnames: array of pingroup names the function uses
  60. * @npgnames: number of pingroup names the function uses
  61. * @node: list node
  62. */
  63. struct pcs_function {
  64. const char *name;
  65. struct pcs_func_vals *vals;
  66. unsigned nvals;
  67. const char **pgnames;
  68. int npgnames;
  69. struct list_head node;
  70. };
  71. /**
  72. * struct pcs_gpio_range - pinctrl gpio range
  73. * @range: subrange of the GPIO number space
  74. * @gpio_func: gpio function value in the pinmux register
  75. */
  76. struct pcs_gpio_range {
  77. struct pinctrl_gpio_range range;
  78. int gpio_func;
  79. };
  80. /**
  81. * struct pcs_data - wrapper for data needed by pinctrl framework
  82. * @pa: pindesc array
  83. * @cur: index to current element
  84. *
  85. * REVISIT: We should be able to drop this eventually by adding
  86. * support for registering pins individually in the pinctrl
  87. * framework for those drivers that don't need a static array.
  88. */
  89. struct pcs_data {
  90. struct pinctrl_pin_desc *pa;
  91. int cur;
  92. };
  93. /**
  94. * struct pcs_name - register name for a pin
  95. * @name: name of the pinctrl register
  96. *
  97. * REVISIT: We may want to make names optional in the pinctrl
  98. * framework as some drivers may not care about pin names to
  99. * avoid kernel bloat. The pin names can be deciphered by user
  100. * space tools using debugfs based on the register address and
  101. * SoC packaging information.
  102. */
  103. struct pcs_name {
  104. char name[PCS_REG_NAME_LEN];
  105. };
  106. /**
  107. * struct pcs_device - pinctrl device instance
  108. * @res: resources
  109. * @base: virtual address of the controller
  110. * @size: size of the ioremapped area
  111. * @dev: device entry
  112. * @pctl: pin controller device
  113. * @mutex: mutex protecting the lists
  114. * @width: bits per mux register
  115. * @fmask: function register mask
  116. * @fshift: function register shift
  117. * @foff: value to turn mux off
  118. * @fmax: max number of functions in fmask
  119. * @names: array of register names for pins
  120. * @pins: physical pins on the SoC
  121. * @pgtree: pingroup index radix tree
  122. * @ftree: function index radix tree
  123. * @pingroups: list of pingroups
  124. * @functions: list of functions
  125. * @ngroups: number of pingroups
  126. * @nfuncs: number of functions
  127. * @desc: pin controller descriptor
  128. * @read: register read function to use
  129. * @write: register write function to use
  130. */
  131. struct pcs_device {
  132. struct resource *res;
  133. void __iomem *base;
  134. unsigned size;
  135. struct device *dev;
  136. struct pinctrl_dev *pctl;
  137. struct mutex mutex;
  138. unsigned width;
  139. unsigned fmask;
  140. unsigned fshift;
  141. unsigned foff;
  142. unsigned fmax;
  143. bool bits_per_mux;
  144. struct pcs_name *names;
  145. struct pcs_data pins;
  146. struct radix_tree_root pgtree;
  147. struct radix_tree_root ftree;
  148. struct list_head pingroups;
  149. struct list_head functions;
  150. unsigned ngroups;
  151. unsigned nfuncs;
  152. struct pinctrl_desc desc;
  153. unsigned (*read)(void __iomem *reg);
  154. void (*write)(unsigned val, void __iomem *reg);
  155. };
  156. /*
  157. * REVISIT: Reads and writes could eventually use regmap or something
  158. * generic. But at least on omaps, some mux registers are performance
  159. * critical as they may need to be remuxed every time before and after
  160. * idle. Adding tests for register access width for every read and
  161. * write like regmap is doing is not desired, and caching the registers
  162. * does not help in this case.
  163. */
  164. static unsigned __maybe_unused pcs_readb(void __iomem *reg)
  165. {
  166. return readb(reg);
  167. }
  168. static unsigned __maybe_unused pcs_readw(void __iomem *reg)
  169. {
  170. return readw(reg);
  171. }
  172. static unsigned __maybe_unused pcs_readl(void __iomem *reg)
  173. {
  174. return readl(reg);
  175. }
  176. static void __maybe_unused pcs_writeb(unsigned val, void __iomem *reg)
  177. {
  178. writeb(val, reg);
  179. }
  180. static void __maybe_unused pcs_writew(unsigned val, void __iomem *reg)
  181. {
  182. writew(val, reg);
  183. }
  184. static void __maybe_unused pcs_writel(unsigned val, void __iomem *reg)
  185. {
  186. writel(val, reg);
  187. }
  188. static int pcs_get_groups_count(struct pinctrl_dev *pctldev)
  189. {
  190. struct pcs_device *pcs;
  191. pcs = pinctrl_dev_get_drvdata(pctldev);
  192. return pcs->ngroups;
  193. }
  194. static const char *pcs_get_group_name(struct pinctrl_dev *pctldev,
  195. unsigned gselector)
  196. {
  197. struct pcs_device *pcs;
  198. struct pcs_pingroup *group;
  199. pcs = pinctrl_dev_get_drvdata(pctldev);
  200. group = radix_tree_lookup(&pcs->pgtree, gselector);
  201. if (!group) {
  202. dev_err(pcs->dev, "%s could not find pingroup%i\n",
  203. __func__, gselector);
  204. return NULL;
  205. }
  206. return group->name;
  207. }
  208. static int pcs_get_group_pins(struct pinctrl_dev *pctldev,
  209. unsigned gselector,
  210. const unsigned **pins,
  211. unsigned *npins)
  212. {
  213. struct pcs_device *pcs;
  214. struct pcs_pingroup *group;
  215. pcs = pinctrl_dev_get_drvdata(pctldev);
  216. group = radix_tree_lookup(&pcs->pgtree, gselector);
  217. if (!group) {
  218. dev_err(pcs->dev, "%s could not find pingroup%i\n",
  219. __func__, gselector);
  220. return -EINVAL;
  221. }
  222. *pins = group->gpins;
  223. *npins = group->ngpins;
  224. return 0;
  225. }
  226. static void pcs_pin_dbg_show(struct pinctrl_dev *pctldev,
  227. struct seq_file *s,
  228. unsigned pin)
  229. {
  230. struct pcs_device *pcs;
  231. unsigned val, mux_bytes;
  232. pcs = pinctrl_dev_get_drvdata(pctldev);
  233. mux_bytes = pcs->width / BITS_PER_BYTE;
  234. val = pcs->read(pcs->base + pin * mux_bytes);
  235. seq_printf(s, "%08x %s " , val, DRIVER_NAME);
  236. }
  237. static void pcs_dt_free_map(struct pinctrl_dev *pctldev,
  238. struct pinctrl_map *map, unsigned num_maps)
  239. {
  240. struct pcs_device *pcs;
  241. pcs = pinctrl_dev_get_drvdata(pctldev);
  242. devm_kfree(pcs->dev, map);
  243. }
  244. static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
  245. struct device_node *np_config,
  246. struct pinctrl_map **map, unsigned *num_maps);
  247. static struct pinctrl_ops pcs_pinctrl_ops = {
  248. .get_groups_count = pcs_get_groups_count,
  249. .get_group_name = pcs_get_group_name,
  250. .get_group_pins = pcs_get_group_pins,
  251. .pin_dbg_show = pcs_pin_dbg_show,
  252. .dt_node_to_map = pcs_dt_node_to_map,
  253. .dt_free_map = pcs_dt_free_map,
  254. };
  255. static int pcs_get_functions_count(struct pinctrl_dev *pctldev)
  256. {
  257. struct pcs_device *pcs;
  258. pcs = pinctrl_dev_get_drvdata(pctldev);
  259. return pcs->nfuncs;
  260. }
  261. static const char *pcs_get_function_name(struct pinctrl_dev *pctldev,
  262. unsigned fselector)
  263. {
  264. struct pcs_device *pcs;
  265. struct pcs_function *func;
  266. pcs = pinctrl_dev_get_drvdata(pctldev);
  267. func = radix_tree_lookup(&pcs->ftree, fselector);
  268. if (!func) {
  269. dev_err(pcs->dev, "%s could not find function%i\n",
  270. __func__, fselector);
  271. return NULL;
  272. }
  273. return func->name;
  274. }
  275. static int pcs_get_function_groups(struct pinctrl_dev *pctldev,
  276. unsigned fselector,
  277. const char * const **groups,
  278. unsigned * const ngroups)
  279. {
  280. struct pcs_device *pcs;
  281. struct pcs_function *func;
  282. pcs = pinctrl_dev_get_drvdata(pctldev);
  283. func = radix_tree_lookup(&pcs->ftree, fselector);
  284. if (!func) {
  285. dev_err(pcs->dev, "%s could not find function%i\n",
  286. __func__, fselector);
  287. return -EINVAL;
  288. }
  289. *groups = func->pgnames;
  290. *ngroups = func->npgnames;
  291. return 0;
  292. }
  293. static int pcs_enable(struct pinctrl_dev *pctldev, unsigned fselector,
  294. unsigned group)
  295. {
  296. struct pcs_device *pcs;
  297. struct pcs_function *func;
  298. int i;
  299. pcs = pinctrl_dev_get_drvdata(pctldev);
  300. func = radix_tree_lookup(&pcs->ftree, fselector);
  301. if (!func)
  302. return -EINVAL;
  303. dev_dbg(pcs->dev, "enabling %s function%i\n",
  304. func->name, fselector);
  305. for (i = 0; i < func->nvals; i++) {
  306. struct pcs_func_vals *vals;
  307. unsigned val, mask;
  308. vals = &func->vals[i];
  309. val = pcs->read(vals->reg);
  310. if (!vals->mask)
  311. mask = pcs->fmask;
  312. else
  313. mask = pcs->fmask & vals->mask;
  314. val &= ~mask;
  315. val |= (vals->val & mask);
  316. pcs->write(val, vals->reg);
  317. }
  318. return 0;
  319. }
  320. static void pcs_disable(struct pinctrl_dev *pctldev, unsigned fselector,
  321. unsigned group)
  322. {
  323. struct pcs_device *pcs;
  324. struct pcs_function *func;
  325. int i;
  326. pcs = pinctrl_dev_get_drvdata(pctldev);
  327. func = radix_tree_lookup(&pcs->ftree, fselector);
  328. if (!func) {
  329. dev_err(pcs->dev, "%s could not find function%i\n",
  330. __func__, fselector);
  331. return;
  332. }
  333. /*
  334. * Ignore disable if function-off is not specified. Some hardware
  335. * does not have clearly defined disable function. For pin specific
  336. * off modes, you can use alternate named states as described in
  337. * pinctrl-bindings.txt.
  338. */
  339. if (pcs->foff == PCS_OFF_DISABLED) {
  340. dev_dbg(pcs->dev, "ignoring disable for %s function%i\n",
  341. func->name, fselector);
  342. return;
  343. }
  344. dev_dbg(pcs->dev, "disabling function%i %s\n",
  345. fselector, func->name);
  346. for (i = 0; i < func->nvals; i++) {
  347. struct pcs_func_vals *vals;
  348. unsigned val;
  349. vals = &func->vals[i];
  350. val = pcs->read(vals->reg);
  351. val &= ~pcs->fmask;
  352. val |= pcs->foff << pcs->fshift;
  353. pcs->write(val, vals->reg);
  354. }
  355. }
  356. static int pcs_request_gpio(struct pinctrl_dev *pctldev,
  357. struct pinctrl_gpio_range *range, unsigned pin)
  358. {
  359. struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
  360. struct pcs_gpio_range *gpio = NULL;
  361. int end, mux_bytes;
  362. unsigned data;
  363. gpio = container_of(range, struct pcs_gpio_range, range);
  364. end = range->pin_base + range->npins - 1;
  365. if (pin < range->pin_base || pin > end) {
  366. dev_err(pctldev->dev,
  367. "pin %d isn't in the range of %d to %d\n",
  368. pin, range->pin_base, end);
  369. return -EINVAL;
  370. }
  371. mux_bytes = pcs->width / BITS_PER_BYTE;
  372. data = pcs->read(pcs->base + pin * mux_bytes) & ~pcs->fmask;
  373. data |= gpio->gpio_func;
  374. pcs->write(data, pcs->base + pin * mux_bytes);
  375. return 0;
  376. }
  377. static struct pinmux_ops pcs_pinmux_ops = {
  378. .get_functions_count = pcs_get_functions_count,
  379. .get_function_name = pcs_get_function_name,
  380. .get_function_groups = pcs_get_function_groups,
  381. .enable = pcs_enable,
  382. .disable = pcs_disable,
  383. .gpio_request_enable = pcs_request_gpio,
  384. };
  385. static int pcs_pinconf_get(struct pinctrl_dev *pctldev,
  386. unsigned pin, unsigned long *config)
  387. {
  388. return -ENOTSUPP;
  389. }
  390. static int pcs_pinconf_set(struct pinctrl_dev *pctldev,
  391. unsigned pin, unsigned long config)
  392. {
  393. return -ENOTSUPP;
  394. }
  395. static int pcs_pinconf_group_get(struct pinctrl_dev *pctldev,
  396. unsigned group, unsigned long *config)
  397. {
  398. return -ENOTSUPP;
  399. }
  400. static int pcs_pinconf_group_set(struct pinctrl_dev *pctldev,
  401. unsigned group, unsigned long config)
  402. {
  403. return -ENOTSUPP;
  404. }
  405. static void pcs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  406. struct seq_file *s, unsigned offset)
  407. {
  408. }
  409. static void pcs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  410. struct seq_file *s, unsigned selector)
  411. {
  412. }
  413. static struct pinconf_ops pcs_pinconf_ops = {
  414. .pin_config_get = pcs_pinconf_get,
  415. .pin_config_set = pcs_pinconf_set,
  416. .pin_config_group_get = pcs_pinconf_group_get,
  417. .pin_config_group_set = pcs_pinconf_group_set,
  418. .pin_config_dbg_show = pcs_pinconf_dbg_show,
  419. .pin_config_group_dbg_show = pcs_pinconf_group_dbg_show,
  420. };
  421. /**
  422. * pcs_add_pin() - add a pin to the static per controller pin array
  423. * @pcs: pcs driver instance
  424. * @offset: register offset from base
  425. */
  426. static int pcs_add_pin(struct pcs_device *pcs, unsigned offset)
  427. {
  428. struct pinctrl_pin_desc *pin;
  429. struct pcs_name *pn;
  430. int i;
  431. i = pcs->pins.cur;
  432. if (i >= pcs->desc.npins) {
  433. dev_err(pcs->dev, "too many pins, max %i\n",
  434. pcs->desc.npins);
  435. return -ENOMEM;
  436. }
  437. pin = &pcs->pins.pa[i];
  438. pn = &pcs->names[i];
  439. sprintf(pn->name, "%lx",
  440. (unsigned long)pcs->res->start + offset);
  441. pin->name = pn->name;
  442. pin->number = i;
  443. pcs->pins.cur++;
  444. return i;
  445. }
  446. /**
  447. * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver
  448. * @pcs: pcs driver instance
  449. *
  450. * In case of errors, resources are freed in pcs_free_resources.
  451. *
  452. * If your hardware needs holes in the address space, then just set
  453. * up multiple driver instances.
  454. */
  455. static int pcs_allocate_pin_table(struct pcs_device *pcs)
  456. {
  457. int mux_bytes, nr_pins, i;
  458. mux_bytes = pcs->width / BITS_PER_BYTE;
  459. nr_pins = pcs->size / mux_bytes;
  460. dev_dbg(pcs->dev, "allocating %i pins\n", nr_pins);
  461. pcs->pins.pa = devm_kzalloc(pcs->dev,
  462. sizeof(*pcs->pins.pa) * nr_pins,
  463. GFP_KERNEL);
  464. if (!pcs->pins.pa)
  465. return -ENOMEM;
  466. pcs->names = devm_kzalloc(pcs->dev,
  467. sizeof(struct pcs_name) * nr_pins,
  468. GFP_KERNEL);
  469. if (!pcs->names)
  470. return -ENOMEM;
  471. pcs->desc.pins = pcs->pins.pa;
  472. pcs->desc.npins = nr_pins;
  473. for (i = 0; i < pcs->desc.npins; i++) {
  474. unsigned offset;
  475. int res;
  476. offset = i * mux_bytes;
  477. res = pcs_add_pin(pcs, offset);
  478. if (res < 0) {
  479. dev_err(pcs->dev, "error adding pins: %i\n", res);
  480. return res;
  481. }
  482. }
  483. return 0;
  484. }
  485. /**
  486. * pcs_add_function() - adds a new function to the function list
  487. * @pcs: pcs driver instance
  488. * @np: device node of the mux entry
  489. * @name: name of the function
  490. * @vals: array of mux register value pairs used by the function
  491. * @nvals: number of mux register value pairs
  492. * @pgnames: array of pingroup names for the function
  493. * @npgnames: number of pingroup names
  494. */
  495. static struct pcs_function *pcs_add_function(struct pcs_device *pcs,
  496. struct device_node *np,
  497. const char *name,
  498. struct pcs_func_vals *vals,
  499. unsigned nvals,
  500. const char **pgnames,
  501. unsigned npgnames)
  502. {
  503. struct pcs_function *function;
  504. function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL);
  505. if (!function)
  506. return NULL;
  507. function->name = name;
  508. function->vals = vals;
  509. function->nvals = nvals;
  510. function->pgnames = pgnames;
  511. function->npgnames = npgnames;
  512. mutex_lock(&pcs->mutex);
  513. list_add_tail(&function->node, &pcs->functions);
  514. radix_tree_insert(&pcs->ftree, pcs->nfuncs, function);
  515. pcs->nfuncs++;
  516. mutex_unlock(&pcs->mutex);
  517. return function;
  518. }
  519. static void pcs_remove_function(struct pcs_device *pcs,
  520. struct pcs_function *function)
  521. {
  522. int i;
  523. mutex_lock(&pcs->mutex);
  524. for (i = 0; i < pcs->nfuncs; i++) {
  525. struct pcs_function *found;
  526. found = radix_tree_lookup(&pcs->ftree, i);
  527. if (found == function)
  528. radix_tree_delete(&pcs->ftree, i);
  529. }
  530. list_del(&function->node);
  531. mutex_unlock(&pcs->mutex);
  532. }
  533. /**
  534. * pcs_add_pingroup() - add a pingroup to the pingroup list
  535. * @pcs: pcs driver instance
  536. * @np: device node of the mux entry
  537. * @name: name of the pingroup
  538. * @gpins: array of the pins that belong to the group
  539. * @ngpins: number of pins in the group
  540. */
  541. static int pcs_add_pingroup(struct pcs_device *pcs,
  542. struct device_node *np,
  543. const char *name,
  544. int *gpins,
  545. int ngpins)
  546. {
  547. struct pcs_pingroup *pingroup;
  548. pingroup = devm_kzalloc(pcs->dev, sizeof(*pingroup), GFP_KERNEL);
  549. if (!pingroup)
  550. return -ENOMEM;
  551. pingroup->name = name;
  552. pingroup->np = np;
  553. pingroup->gpins = gpins;
  554. pingroup->ngpins = ngpins;
  555. mutex_lock(&pcs->mutex);
  556. list_add_tail(&pingroup->node, &pcs->pingroups);
  557. radix_tree_insert(&pcs->pgtree, pcs->ngroups, pingroup);
  558. pcs->ngroups++;
  559. mutex_unlock(&pcs->mutex);
  560. return 0;
  561. }
  562. /**
  563. * pcs_get_pin_by_offset() - get a pin index based on the register offset
  564. * @pcs: pcs driver instance
  565. * @offset: register offset from the base
  566. *
  567. * Note that this is OK as long as the pins are in a static array.
  568. */
  569. static int pcs_get_pin_by_offset(struct pcs_device *pcs, unsigned offset)
  570. {
  571. unsigned index;
  572. if (offset >= pcs->size) {
  573. dev_err(pcs->dev, "mux offset out of range: 0x%x (0x%x)\n",
  574. offset, pcs->size);
  575. return -EINVAL;
  576. }
  577. index = offset / (pcs->width / BITS_PER_BYTE);
  578. return index;
  579. }
  580. /**
  581. * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
  582. * @pcs: pinctrl driver instance
  583. * @np: device node of the mux entry
  584. * @map: map entry
  585. * @pgnames: pingroup names
  586. *
  587. * Note that this binding currently supports only sets of one register + value.
  588. *
  589. * Also note that this driver tries to avoid understanding pin and function
  590. * names because of the extra bloat they would cause especially in the case of
  591. * a large number of pins. This driver just sets what is specified for the board
  592. * in the .dts file. Further user space debugging tools can be developed to
  593. * decipher the pin and function names using debugfs.
  594. *
  595. * If you are concerned about the boot time, set up the static pins in
  596. * the bootloader, and only set up selected pins as device tree entries.
  597. */
  598. static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
  599. struct device_node *np,
  600. struct pinctrl_map **map,
  601. const char **pgnames)
  602. {
  603. struct pcs_func_vals *vals;
  604. const __be32 *mux;
  605. int size, params, rows, *pins, index = 0, found = 0, res = -ENOMEM;
  606. struct pcs_function *function;
  607. if (pcs->bits_per_mux) {
  608. params = 3;
  609. mux = of_get_property(np, PCS_MUX_BITS_NAME, &size);
  610. } else {
  611. params = 2;
  612. mux = of_get_property(np, PCS_MUX_PINS_NAME, &size);
  613. }
  614. if (!mux) {
  615. dev_err(pcs->dev, "no valid property for %s\n", np->name);
  616. return -EINVAL;
  617. }
  618. if (size < (sizeof(*mux) * params)) {
  619. dev_err(pcs->dev, "bad data for %s\n", np->name);
  620. return -EINVAL;
  621. }
  622. size /= sizeof(*mux); /* Number of elements in array */
  623. rows = size / params;
  624. vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL);
  625. if (!vals)
  626. return -ENOMEM;
  627. pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL);
  628. if (!pins)
  629. goto free_vals;
  630. while (index < size) {
  631. unsigned offset, val;
  632. int pin;
  633. offset = be32_to_cpup(mux + index++);
  634. val = be32_to_cpup(mux + index++);
  635. vals[found].reg = pcs->base + offset;
  636. vals[found].val = val;
  637. if (params == 3) {
  638. val = be32_to_cpup(mux + index++);
  639. vals[found].mask = val;
  640. }
  641. pin = pcs_get_pin_by_offset(pcs, offset);
  642. if (pin < 0) {
  643. dev_err(pcs->dev,
  644. "could not add functions for %s %ux\n",
  645. np->name, offset);
  646. break;
  647. }
  648. pins[found++] = pin;
  649. }
  650. pgnames[0] = np->name;
  651. function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
  652. if (!function)
  653. goto free_pins;
  654. res = pcs_add_pingroup(pcs, np, np->name, pins, found);
  655. if (res < 0)
  656. goto free_function;
  657. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  658. (*map)->data.mux.group = np->name;
  659. (*map)->data.mux.function = np->name;
  660. return 0;
  661. free_function:
  662. pcs_remove_function(pcs, function);
  663. free_pins:
  664. devm_kfree(pcs->dev, pins);
  665. free_vals:
  666. devm_kfree(pcs->dev, vals);
  667. return res;
  668. }
  669. /**
  670. * pcs_dt_node_to_map() - allocates and parses pinctrl maps
  671. * @pctldev: pinctrl instance
  672. * @np_config: device tree pinmux entry
  673. * @map: array of map entries
  674. * @num_maps: number of maps
  675. */
  676. static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
  677. struct device_node *np_config,
  678. struct pinctrl_map **map, unsigned *num_maps)
  679. {
  680. struct pcs_device *pcs;
  681. const char **pgnames;
  682. int ret;
  683. pcs = pinctrl_dev_get_drvdata(pctldev);
  684. *map = devm_kzalloc(pcs->dev, sizeof(**map), GFP_KERNEL);
  685. if (!*map)
  686. return -ENOMEM;
  687. *num_maps = 0;
  688. pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL);
  689. if (!pgnames) {
  690. ret = -ENOMEM;
  691. goto free_map;
  692. }
  693. ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map, pgnames);
  694. if (ret < 0) {
  695. dev_err(pcs->dev, "no pins entries for %s\n",
  696. np_config->name);
  697. goto free_pgnames;
  698. }
  699. *num_maps = 1;
  700. return 0;
  701. free_pgnames:
  702. devm_kfree(pcs->dev, pgnames);
  703. free_map:
  704. devm_kfree(pcs->dev, *map);
  705. return ret;
  706. }
  707. /**
  708. * pcs_free_funcs() - free memory used by functions
  709. * @pcs: pcs driver instance
  710. */
  711. static void pcs_free_funcs(struct pcs_device *pcs)
  712. {
  713. struct list_head *pos, *tmp;
  714. int i;
  715. mutex_lock(&pcs->mutex);
  716. for (i = 0; i < pcs->nfuncs; i++) {
  717. struct pcs_function *func;
  718. func = radix_tree_lookup(&pcs->ftree, i);
  719. if (!func)
  720. continue;
  721. radix_tree_delete(&pcs->ftree, i);
  722. }
  723. list_for_each_safe(pos, tmp, &pcs->functions) {
  724. struct pcs_function *function;
  725. function = list_entry(pos, struct pcs_function, node);
  726. list_del(&function->node);
  727. }
  728. mutex_unlock(&pcs->mutex);
  729. }
  730. /**
  731. * pcs_free_pingroups() - free memory used by pingroups
  732. * @pcs: pcs driver instance
  733. */
  734. static void pcs_free_pingroups(struct pcs_device *pcs)
  735. {
  736. struct list_head *pos, *tmp;
  737. int i;
  738. mutex_lock(&pcs->mutex);
  739. for (i = 0; i < pcs->ngroups; i++) {
  740. struct pcs_pingroup *pingroup;
  741. pingroup = radix_tree_lookup(&pcs->pgtree, i);
  742. if (!pingroup)
  743. continue;
  744. radix_tree_delete(&pcs->pgtree, i);
  745. }
  746. list_for_each_safe(pos, tmp, &pcs->pingroups) {
  747. struct pcs_pingroup *pingroup;
  748. pingroup = list_entry(pos, struct pcs_pingroup, node);
  749. list_del(&pingroup->node);
  750. }
  751. mutex_unlock(&pcs->mutex);
  752. }
  753. /**
  754. * pcs_free_resources() - free memory used by this driver
  755. * @pcs: pcs driver instance
  756. */
  757. static void pcs_free_resources(struct pcs_device *pcs)
  758. {
  759. if (pcs->pctl)
  760. pinctrl_unregister(pcs->pctl);
  761. pcs_free_funcs(pcs);
  762. pcs_free_pingroups(pcs);
  763. }
  764. #define PCS_GET_PROP_U32(name, reg, err) \
  765. do { \
  766. ret = of_property_read_u32(np, name, reg); \
  767. if (ret) { \
  768. dev_err(pcs->dev, err); \
  769. return ret; \
  770. } \
  771. } while (0);
  772. static struct of_device_id pcs_of_match[];
  773. static int pcs_add_gpio_range(struct device_node *node, struct pcs_device *pcs)
  774. {
  775. struct pcs_gpio_range *gpio;
  776. struct device_node *child;
  777. struct resource r;
  778. const char name[] = "pinctrl-single";
  779. u32 gpiores[PCS_MAX_GPIO_VALUES];
  780. int ret, i = 0, mux_bytes = 0;
  781. for_each_child_of_node(node, child) {
  782. ret = of_address_to_resource(child, 0, &r);
  783. if (ret < 0)
  784. continue;
  785. memset(gpiores, 0, sizeof(u32) * PCS_MAX_GPIO_VALUES);
  786. ret = of_property_read_u32_array(child, "pinctrl-single,gpio",
  787. gpiores, PCS_MAX_GPIO_VALUES);
  788. if (ret < 0)
  789. continue;
  790. gpio = devm_kzalloc(pcs->dev, sizeof(*gpio), GFP_KERNEL);
  791. if (!gpio) {
  792. dev_err(pcs->dev, "failed to allocate pcs gpio\n");
  793. return -ENOMEM;
  794. }
  795. gpio->range.name = devm_kzalloc(pcs->dev, sizeof(name),
  796. GFP_KERNEL);
  797. if (!gpio->range.name) {
  798. dev_err(pcs->dev, "failed to allocate range name\n");
  799. return -ENOMEM;
  800. }
  801. memcpy((char *)gpio->range.name, name, sizeof(name));
  802. gpio->range.id = i++;
  803. gpio->range.base = gpiores[0];
  804. gpio->gpio_func = gpiores[1];
  805. mux_bytes = pcs->width / BITS_PER_BYTE;
  806. gpio->range.pin_base = (r.start - pcs->res->start) / mux_bytes;
  807. gpio->range.npins = (r.end - r.start) / mux_bytes + 1;
  808. pinctrl_add_gpio_range(pcs->pctl, &gpio->range);
  809. }
  810. return 0;
  811. }
  812. static int pcs_probe(struct platform_device *pdev)
  813. {
  814. struct device_node *np = pdev->dev.of_node;
  815. const struct of_device_id *match;
  816. struct resource *res;
  817. struct pcs_device *pcs;
  818. int ret;
  819. match = of_match_device(pcs_of_match, &pdev->dev);
  820. if (!match)
  821. return -EINVAL;
  822. pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL);
  823. if (!pcs) {
  824. dev_err(&pdev->dev, "could not allocate\n");
  825. return -ENOMEM;
  826. }
  827. pcs->dev = &pdev->dev;
  828. mutex_init(&pcs->mutex);
  829. INIT_LIST_HEAD(&pcs->pingroups);
  830. INIT_LIST_HEAD(&pcs->functions);
  831. PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs->width,
  832. "register width not specified\n");
  833. PCS_GET_PROP_U32("pinctrl-single,function-mask", &pcs->fmask,
  834. "function register mask not specified\n");
  835. pcs->fshift = ffs(pcs->fmask) - 1;
  836. pcs->fmax = pcs->fmask >> pcs->fshift;
  837. ret = of_property_read_u32(np, "pinctrl-single,function-off",
  838. &pcs->foff);
  839. if (ret)
  840. pcs->foff = PCS_OFF_DISABLED;
  841. pcs->bits_per_mux = of_property_read_bool(np,
  842. "pinctrl-single,bit-per-mux");
  843. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  844. if (!res) {
  845. dev_err(pcs->dev, "could not get resource\n");
  846. return -ENODEV;
  847. }
  848. pcs->res = devm_request_mem_region(pcs->dev, res->start,
  849. resource_size(res), DRIVER_NAME);
  850. if (!pcs->res) {
  851. dev_err(pcs->dev, "could not get mem_region\n");
  852. return -EBUSY;
  853. }
  854. pcs->size = resource_size(pcs->res);
  855. pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size);
  856. if (!pcs->base) {
  857. dev_err(pcs->dev, "could not ioremap\n");
  858. return -ENODEV;
  859. }
  860. INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL);
  861. INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL);
  862. platform_set_drvdata(pdev, pcs);
  863. switch (pcs->width) {
  864. case 8:
  865. pcs->read = pcs_readb;
  866. pcs->write = pcs_writeb;
  867. break;
  868. case 16:
  869. pcs->read = pcs_readw;
  870. pcs->write = pcs_writew;
  871. break;
  872. case 32:
  873. pcs->read = pcs_readl;
  874. pcs->write = pcs_writel;
  875. break;
  876. default:
  877. break;
  878. }
  879. pcs->desc.name = DRIVER_NAME;
  880. pcs->desc.pctlops = &pcs_pinctrl_ops;
  881. pcs->desc.pmxops = &pcs_pinmux_ops;
  882. pcs->desc.confops = &pcs_pinconf_ops;
  883. pcs->desc.owner = THIS_MODULE;
  884. ret = pcs_allocate_pin_table(pcs);
  885. if (ret < 0)
  886. goto free;
  887. pcs->pctl = pinctrl_register(&pcs->desc, pcs->dev, pcs);
  888. if (!pcs->pctl) {
  889. dev_err(pcs->dev, "could not register single pinctrl driver\n");
  890. ret = -EINVAL;
  891. goto free;
  892. }
  893. ret = pcs_add_gpio_range(np, pcs);
  894. if (ret < 0)
  895. goto free;
  896. dev_info(pcs->dev, "%i pins at pa %p size %u\n",
  897. pcs->desc.npins, pcs->base, pcs->size);
  898. return 0;
  899. free:
  900. pcs_free_resources(pcs);
  901. return ret;
  902. }
  903. static int pcs_remove(struct platform_device *pdev)
  904. {
  905. struct pcs_device *pcs = platform_get_drvdata(pdev);
  906. if (!pcs)
  907. return 0;
  908. pcs_free_resources(pcs);
  909. return 0;
  910. }
  911. static struct of_device_id pcs_of_match[] = {
  912. { .compatible = DRIVER_NAME, },
  913. { },
  914. };
  915. MODULE_DEVICE_TABLE(of, pcs_of_match);
  916. static struct platform_driver pcs_driver = {
  917. .probe = pcs_probe,
  918. .remove = pcs_remove,
  919. .driver = {
  920. .owner = THIS_MODULE,
  921. .name = DRIVER_NAME,
  922. .of_match_table = pcs_of_match,
  923. },
  924. };
  925. module_platform_driver(pcs_driver);
  926. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  927. MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver");
  928. MODULE_LICENSE("GPL v2");