pinctrl-single.c 26 KB

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