pinctrl-single.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. /* If function mask is null, needn't enable it. */
  306. if (!pcs->fmask)
  307. return 0;
  308. func = radix_tree_lookup(&pcs->ftree, fselector);
  309. if (!func)
  310. return -EINVAL;
  311. dev_dbg(pcs->dev, "enabling %s function%i\n",
  312. func->name, fselector);
  313. for (i = 0; i < func->nvals; i++) {
  314. struct pcs_func_vals *vals;
  315. unsigned val, mask;
  316. vals = &func->vals[i];
  317. val = pcs->read(vals->reg);
  318. if (!vals->mask)
  319. mask = pcs->fmask;
  320. else
  321. mask = pcs->fmask & vals->mask;
  322. val &= ~mask;
  323. val |= (vals->val & mask);
  324. pcs->write(val, vals->reg);
  325. }
  326. return 0;
  327. }
  328. static void pcs_disable(struct pinctrl_dev *pctldev, unsigned fselector,
  329. unsigned group)
  330. {
  331. struct pcs_device *pcs;
  332. struct pcs_function *func;
  333. int i;
  334. pcs = pinctrl_dev_get_drvdata(pctldev);
  335. /* If function mask is null, needn't disable it. */
  336. if (!pcs->fmask)
  337. return;
  338. func = radix_tree_lookup(&pcs->ftree, fselector);
  339. if (!func) {
  340. dev_err(pcs->dev, "%s could not find function%i\n",
  341. __func__, fselector);
  342. return;
  343. }
  344. /*
  345. * Ignore disable if function-off is not specified. Some hardware
  346. * does not have clearly defined disable function. For pin specific
  347. * off modes, you can use alternate named states as described in
  348. * pinctrl-bindings.txt.
  349. */
  350. if (pcs->foff == PCS_OFF_DISABLED) {
  351. dev_dbg(pcs->dev, "ignoring disable for %s function%i\n",
  352. func->name, fselector);
  353. return;
  354. }
  355. dev_dbg(pcs->dev, "disabling function%i %s\n",
  356. fselector, func->name);
  357. for (i = 0; i < func->nvals; i++) {
  358. struct pcs_func_vals *vals;
  359. unsigned val;
  360. vals = &func->vals[i];
  361. val = pcs->read(vals->reg);
  362. val &= ~pcs->fmask;
  363. val |= pcs->foff << pcs->fshift;
  364. pcs->write(val, vals->reg);
  365. }
  366. }
  367. static int pcs_request_gpio(struct pinctrl_dev *pctldev,
  368. struct pinctrl_gpio_range *range, unsigned pin)
  369. {
  370. struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
  371. struct pcs_gpiofunc_range *frange = NULL;
  372. struct list_head *pos, *tmp;
  373. int mux_bytes = 0;
  374. unsigned data;
  375. /* If function mask is null, return directly. */
  376. if (!pcs->fmask)
  377. return -ENOTSUPP;
  378. list_for_each_safe(pos, tmp, &pcs->gpiofuncs) {
  379. frange = list_entry(pos, struct pcs_gpiofunc_range, node);
  380. if (pin >= frange->offset + frange->npins
  381. || pin < frange->offset)
  382. continue;
  383. mux_bytes = pcs->width / BITS_PER_BYTE;
  384. data = pcs->read(pcs->base + pin * mux_bytes) & ~pcs->fmask;
  385. data |= frange->gpiofunc;
  386. pcs->write(data, pcs->base + pin * mux_bytes);
  387. break;
  388. }
  389. return 0;
  390. }
  391. static const struct pinmux_ops pcs_pinmux_ops = {
  392. .get_functions_count = pcs_get_functions_count,
  393. .get_function_name = pcs_get_function_name,
  394. .get_function_groups = pcs_get_function_groups,
  395. .enable = pcs_enable,
  396. .disable = pcs_disable,
  397. .gpio_request_enable = pcs_request_gpio,
  398. };
  399. static int pcs_pinconf_get(struct pinctrl_dev *pctldev,
  400. unsigned pin, unsigned long *config)
  401. {
  402. return -ENOTSUPP;
  403. }
  404. static int pcs_pinconf_set(struct pinctrl_dev *pctldev,
  405. unsigned pin, unsigned long config)
  406. {
  407. return -ENOTSUPP;
  408. }
  409. static int pcs_pinconf_group_get(struct pinctrl_dev *pctldev,
  410. unsigned group, unsigned long *config)
  411. {
  412. return -ENOTSUPP;
  413. }
  414. static int pcs_pinconf_group_set(struct pinctrl_dev *pctldev,
  415. unsigned group, unsigned long config)
  416. {
  417. return -ENOTSUPP;
  418. }
  419. static void pcs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  420. struct seq_file *s, unsigned offset)
  421. {
  422. }
  423. static void pcs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  424. struct seq_file *s, unsigned selector)
  425. {
  426. }
  427. static const struct pinconf_ops pcs_pinconf_ops = {
  428. .pin_config_get = pcs_pinconf_get,
  429. .pin_config_set = pcs_pinconf_set,
  430. .pin_config_group_get = pcs_pinconf_group_get,
  431. .pin_config_group_set = pcs_pinconf_group_set,
  432. .pin_config_dbg_show = pcs_pinconf_dbg_show,
  433. .pin_config_group_dbg_show = pcs_pinconf_group_dbg_show,
  434. };
  435. /**
  436. * pcs_add_pin() - add a pin to the static per controller pin array
  437. * @pcs: pcs driver instance
  438. * @offset: register offset from base
  439. */
  440. static int pcs_add_pin(struct pcs_device *pcs, unsigned offset)
  441. {
  442. struct pinctrl_pin_desc *pin;
  443. struct pcs_name *pn;
  444. int i;
  445. i = pcs->pins.cur;
  446. if (i >= pcs->desc.npins) {
  447. dev_err(pcs->dev, "too many pins, max %i\n",
  448. pcs->desc.npins);
  449. return -ENOMEM;
  450. }
  451. pin = &pcs->pins.pa[i];
  452. pn = &pcs->names[i];
  453. sprintf(pn->name, "%lx",
  454. (unsigned long)pcs->res->start + offset);
  455. pin->name = pn->name;
  456. pin->number = i;
  457. pcs->pins.cur++;
  458. return i;
  459. }
  460. /**
  461. * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver
  462. * @pcs: pcs driver instance
  463. *
  464. * In case of errors, resources are freed in pcs_free_resources.
  465. *
  466. * If your hardware needs holes in the address space, then just set
  467. * up multiple driver instances.
  468. */
  469. static int pcs_allocate_pin_table(struct pcs_device *pcs)
  470. {
  471. int mux_bytes, nr_pins, i;
  472. mux_bytes = pcs->width / BITS_PER_BYTE;
  473. nr_pins = pcs->size / mux_bytes;
  474. dev_dbg(pcs->dev, "allocating %i pins\n", nr_pins);
  475. pcs->pins.pa = devm_kzalloc(pcs->dev,
  476. sizeof(*pcs->pins.pa) * nr_pins,
  477. GFP_KERNEL);
  478. if (!pcs->pins.pa)
  479. return -ENOMEM;
  480. pcs->names = devm_kzalloc(pcs->dev,
  481. sizeof(struct pcs_name) * nr_pins,
  482. GFP_KERNEL);
  483. if (!pcs->names)
  484. return -ENOMEM;
  485. pcs->desc.pins = pcs->pins.pa;
  486. pcs->desc.npins = nr_pins;
  487. for (i = 0; i < pcs->desc.npins; i++) {
  488. unsigned offset;
  489. int res;
  490. offset = i * mux_bytes;
  491. res = pcs_add_pin(pcs, offset);
  492. if (res < 0) {
  493. dev_err(pcs->dev, "error adding pins: %i\n", res);
  494. return res;
  495. }
  496. }
  497. return 0;
  498. }
  499. /**
  500. * pcs_add_function() - adds a new function to the function list
  501. * @pcs: pcs driver instance
  502. * @np: device node of the mux entry
  503. * @name: name of the function
  504. * @vals: array of mux register value pairs used by the function
  505. * @nvals: number of mux register value pairs
  506. * @pgnames: array of pingroup names for the function
  507. * @npgnames: number of pingroup names
  508. */
  509. static struct pcs_function *pcs_add_function(struct pcs_device *pcs,
  510. struct device_node *np,
  511. const char *name,
  512. struct pcs_func_vals *vals,
  513. unsigned nvals,
  514. const char **pgnames,
  515. unsigned npgnames)
  516. {
  517. struct pcs_function *function;
  518. function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL);
  519. if (!function)
  520. return NULL;
  521. function->name = name;
  522. function->vals = vals;
  523. function->nvals = nvals;
  524. function->pgnames = pgnames;
  525. function->npgnames = npgnames;
  526. mutex_lock(&pcs->mutex);
  527. list_add_tail(&function->node, &pcs->functions);
  528. radix_tree_insert(&pcs->ftree, pcs->nfuncs, function);
  529. pcs->nfuncs++;
  530. mutex_unlock(&pcs->mutex);
  531. return function;
  532. }
  533. static void pcs_remove_function(struct pcs_device *pcs,
  534. struct pcs_function *function)
  535. {
  536. int i;
  537. mutex_lock(&pcs->mutex);
  538. for (i = 0; i < pcs->nfuncs; i++) {
  539. struct pcs_function *found;
  540. found = radix_tree_lookup(&pcs->ftree, i);
  541. if (found == function)
  542. radix_tree_delete(&pcs->ftree, i);
  543. }
  544. list_del(&function->node);
  545. mutex_unlock(&pcs->mutex);
  546. }
  547. /**
  548. * pcs_add_pingroup() - add a pingroup to the pingroup list
  549. * @pcs: pcs driver instance
  550. * @np: device node of the mux entry
  551. * @name: name of the pingroup
  552. * @gpins: array of the pins that belong to the group
  553. * @ngpins: number of pins in the group
  554. */
  555. static int pcs_add_pingroup(struct pcs_device *pcs,
  556. struct device_node *np,
  557. const char *name,
  558. int *gpins,
  559. int ngpins)
  560. {
  561. struct pcs_pingroup *pingroup;
  562. pingroup = devm_kzalloc(pcs->dev, sizeof(*pingroup), GFP_KERNEL);
  563. if (!pingroup)
  564. return -ENOMEM;
  565. pingroup->name = name;
  566. pingroup->np = np;
  567. pingroup->gpins = gpins;
  568. pingroup->ngpins = ngpins;
  569. mutex_lock(&pcs->mutex);
  570. list_add_tail(&pingroup->node, &pcs->pingroups);
  571. radix_tree_insert(&pcs->pgtree, pcs->ngroups, pingroup);
  572. pcs->ngroups++;
  573. mutex_unlock(&pcs->mutex);
  574. return 0;
  575. }
  576. /**
  577. * pcs_get_pin_by_offset() - get a pin index based on the register offset
  578. * @pcs: pcs driver instance
  579. * @offset: register offset from the base
  580. *
  581. * Note that this is OK as long as the pins are in a static array.
  582. */
  583. static int pcs_get_pin_by_offset(struct pcs_device *pcs, unsigned offset)
  584. {
  585. unsigned index;
  586. if (offset >= pcs->size) {
  587. dev_err(pcs->dev, "mux offset out of range: 0x%x (0x%x)\n",
  588. offset, pcs->size);
  589. return -EINVAL;
  590. }
  591. index = offset / (pcs->width / BITS_PER_BYTE);
  592. return index;
  593. }
  594. /**
  595. * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
  596. * @pcs: pinctrl driver instance
  597. * @np: device node of the mux entry
  598. * @map: map entry
  599. * @pgnames: pingroup names
  600. *
  601. * Note that this binding currently supports only sets of one register + value.
  602. *
  603. * Also note that this driver tries to avoid understanding pin and function
  604. * names because of the extra bloat they would cause especially in the case of
  605. * a large number of pins. This driver just sets what is specified for the board
  606. * in the .dts file. Further user space debugging tools can be developed to
  607. * decipher the pin and function names using debugfs.
  608. *
  609. * If you are concerned about the boot time, set up the static pins in
  610. * the bootloader, and only set up selected pins as device tree entries.
  611. */
  612. static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
  613. struct device_node *np,
  614. struct pinctrl_map **map,
  615. const char **pgnames)
  616. {
  617. struct pcs_func_vals *vals;
  618. const __be32 *mux;
  619. int size, params, rows, *pins, index = 0, found = 0, res = -ENOMEM;
  620. struct pcs_function *function;
  621. if (pcs->bits_per_mux) {
  622. params = 3;
  623. mux = of_get_property(np, PCS_MUX_BITS_NAME, &size);
  624. } else {
  625. params = 2;
  626. mux = of_get_property(np, PCS_MUX_PINS_NAME, &size);
  627. }
  628. if (!mux) {
  629. dev_err(pcs->dev, "no valid property for %s\n", np->name);
  630. return -EINVAL;
  631. }
  632. if (size < (sizeof(*mux) * params)) {
  633. dev_err(pcs->dev, "bad data for %s\n", np->name);
  634. return -EINVAL;
  635. }
  636. size /= sizeof(*mux); /* Number of elements in array */
  637. rows = size / params;
  638. vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL);
  639. if (!vals)
  640. return -ENOMEM;
  641. pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL);
  642. if (!pins)
  643. goto free_vals;
  644. while (index < size) {
  645. unsigned offset, val;
  646. int pin;
  647. offset = be32_to_cpup(mux + index++);
  648. val = be32_to_cpup(mux + index++);
  649. vals[found].reg = pcs->base + offset;
  650. vals[found].val = val;
  651. if (params == 3) {
  652. val = be32_to_cpup(mux + index++);
  653. vals[found].mask = val;
  654. }
  655. pin = pcs_get_pin_by_offset(pcs, offset);
  656. if (pin < 0) {
  657. dev_err(pcs->dev,
  658. "could not add functions for %s %ux\n",
  659. np->name, offset);
  660. break;
  661. }
  662. pins[found++] = pin;
  663. }
  664. pgnames[0] = np->name;
  665. function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
  666. if (!function)
  667. goto free_pins;
  668. res = pcs_add_pingroup(pcs, np, np->name, pins, found);
  669. if (res < 0)
  670. goto free_function;
  671. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  672. (*map)->data.mux.group = np->name;
  673. (*map)->data.mux.function = np->name;
  674. return 0;
  675. free_function:
  676. pcs_remove_function(pcs, function);
  677. free_pins:
  678. devm_kfree(pcs->dev, pins);
  679. free_vals:
  680. devm_kfree(pcs->dev, vals);
  681. return res;
  682. }
  683. /**
  684. * pcs_dt_node_to_map() - allocates and parses pinctrl maps
  685. * @pctldev: pinctrl instance
  686. * @np_config: device tree pinmux entry
  687. * @map: array of map entries
  688. * @num_maps: number of maps
  689. */
  690. static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
  691. struct device_node *np_config,
  692. struct pinctrl_map **map, unsigned *num_maps)
  693. {
  694. struct pcs_device *pcs;
  695. const char **pgnames;
  696. int ret;
  697. pcs = pinctrl_dev_get_drvdata(pctldev);
  698. *map = devm_kzalloc(pcs->dev, sizeof(**map), GFP_KERNEL);
  699. if (!*map)
  700. return -ENOMEM;
  701. *num_maps = 0;
  702. pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL);
  703. if (!pgnames) {
  704. ret = -ENOMEM;
  705. goto free_map;
  706. }
  707. ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map, pgnames);
  708. if (ret < 0) {
  709. dev_err(pcs->dev, "no pins entries for %s\n",
  710. np_config->name);
  711. goto free_pgnames;
  712. }
  713. *num_maps = 1;
  714. return 0;
  715. free_pgnames:
  716. devm_kfree(pcs->dev, pgnames);
  717. free_map:
  718. devm_kfree(pcs->dev, *map);
  719. return ret;
  720. }
  721. /**
  722. * pcs_free_funcs() - free memory used by functions
  723. * @pcs: pcs driver instance
  724. */
  725. static void pcs_free_funcs(struct pcs_device *pcs)
  726. {
  727. struct list_head *pos, *tmp;
  728. int i;
  729. mutex_lock(&pcs->mutex);
  730. for (i = 0; i < pcs->nfuncs; i++) {
  731. struct pcs_function *func;
  732. func = radix_tree_lookup(&pcs->ftree, i);
  733. if (!func)
  734. continue;
  735. radix_tree_delete(&pcs->ftree, i);
  736. }
  737. list_for_each_safe(pos, tmp, &pcs->functions) {
  738. struct pcs_function *function;
  739. function = list_entry(pos, struct pcs_function, node);
  740. list_del(&function->node);
  741. }
  742. mutex_unlock(&pcs->mutex);
  743. }
  744. /**
  745. * pcs_free_pingroups() - free memory used by pingroups
  746. * @pcs: pcs driver instance
  747. */
  748. static void pcs_free_pingroups(struct pcs_device *pcs)
  749. {
  750. struct list_head *pos, *tmp;
  751. int i;
  752. mutex_lock(&pcs->mutex);
  753. for (i = 0; i < pcs->ngroups; i++) {
  754. struct pcs_pingroup *pingroup;
  755. pingroup = radix_tree_lookup(&pcs->pgtree, i);
  756. if (!pingroup)
  757. continue;
  758. radix_tree_delete(&pcs->pgtree, i);
  759. }
  760. list_for_each_safe(pos, tmp, &pcs->pingroups) {
  761. struct pcs_pingroup *pingroup;
  762. pingroup = list_entry(pos, struct pcs_pingroup, node);
  763. list_del(&pingroup->node);
  764. }
  765. mutex_unlock(&pcs->mutex);
  766. }
  767. /**
  768. * pcs_free_resources() - free memory used by this driver
  769. * @pcs: pcs driver instance
  770. */
  771. static void pcs_free_resources(struct pcs_device *pcs)
  772. {
  773. if (pcs->pctl)
  774. pinctrl_unregister(pcs->pctl);
  775. pcs_free_funcs(pcs);
  776. pcs_free_pingroups(pcs);
  777. }
  778. #define PCS_GET_PROP_U32(name, reg, err) \
  779. do { \
  780. ret = of_property_read_u32(np, name, reg); \
  781. if (ret) { \
  782. dev_err(pcs->dev, err); \
  783. return ret; \
  784. } \
  785. } while (0);
  786. static struct of_device_id pcs_of_match[];
  787. static int pcs_add_gpio_func(struct device_node *node, struct pcs_device *pcs)
  788. {
  789. const char *propname = "pinctrl-single,gpio-range";
  790. const char *cellname = "#pinctrl-single,gpio-range-cells";
  791. struct of_phandle_args gpiospec;
  792. struct pcs_gpiofunc_range *range;
  793. int ret, i;
  794. for (i = 0; ; i++) {
  795. ret = of_parse_phandle_with_args(node, propname, cellname,
  796. i, &gpiospec);
  797. /* Do not treat it as error. Only treat it as end condition. */
  798. if (ret) {
  799. ret = 0;
  800. break;
  801. }
  802. range = devm_kzalloc(pcs->dev, sizeof(*range), GFP_KERNEL);
  803. if (!range) {
  804. ret = -ENOMEM;
  805. break;
  806. }
  807. range->offset = gpiospec.args[0];
  808. range->npins = gpiospec.args[1];
  809. range->gpiofunc = gpiospec.args[2];
  810. mutex_lock(&pcs->mutex);
  811. list_add_tail(&range->node, &pcs->gpiofuncs);
  812. mutex_unlock(&pcs->mutex);
  813. }
  814. return ret;
  815. }
  816. static int pcs_probe(struct platform_device *pdev)
  817. {
  818. struct device_node *np = pdev->dev.of_node;
  819. const struct of_device_id *match;
  820. struct resource *res;
  821. struct pcs_device *pcs;
  822. int ret;
  823. match = of_match_device(pcs_of_match, &pdev->dev);
  824. if (!match)
  825. return -EINVAL;
  826. pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL);
  827. if (!pcs) {
  828. dev_err(&pdev->dev, "could not allocate\n");
  829. return -ENOMEM;
  830. }
  831. pcs->dev = &pdev->dev;
  832. mutex_init(&pcs->mutex);
  833. INIT_LIST_HEAD(&pcs->pingroups);
  834. INIT_LIST_HEAD(&pcs->functions);
  835. INIT_LIST_HEAD(&pcs->gpiofuncs);
  836. PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs->width,
  837. "register width not specified\n");
  838. ret = of_property_read_u32(np, "pinctrl-single,function-mask",
  839. &pcs->fmask);
  840. if (!ret) {
  841. pcs->fshift = ffs(pcs->fmask) - 1;
  842. pcs->fmax = pcs->fmask >> pcs->fshift;
  843. } else {
  844. /* If mask property doesn't exist, function mux is invalid. */
  845. pcs->fmask = 0;
  846. pcs->fshift = 0;
  847. pcs->fmax = 0;
  848. }
  849. ret = of_property_read_u32(np, "pinctrl-single,function-off",
  850. &pcs->foff);
  851. if (ret)
  852. pcs->foff = PCS_OFF_DISABLED;
  853. pcs->bits_per_mux = of_property_read_bool(np,
  854. "pinctrl-single,bit-per-mux");
  855. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  856. if (!res) {
  857. dev_err(pcs->dev, "could not get resource\n");
  858. return -ENODEV;
  859. }
  860. pcs->res = devm_request_mem_region(pcs->dev, res->start,
  861. resource_size(res), DRIVER_NAME);
  862. if (!pcs->res) {
  863. dev_err(pcs->dev, "could not get mem_region\n");
  864. return -EBUSY;
  865. }
  866. pcs->size = resource_size(pcs->res);
  867. pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size);
  868. if (!pcs->base) {
  869. dev_err(pcs->dev, "could not ioremap\n");
  870. return -ENODEV;
  871. }
  872. INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL);
  873. INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL);
  874. platform_set_drvdata(pdev, pcs);
  875. switch (pcs->width) {
  876. case 8:
  877. pcs->read = pcs_readb;
  878. pcs->write = pcs_writeb;
  879. break;
  880. case 16:
  881. pcs->read = pcs_readw;
  882. pcs->write = pcs_writew;
  883. break;
  884. case 32:
  885. pcs->read = pcs_readl;
  886. pcs->write = pcs_writel;
  887. break;
  888. default:
  889. break;
  890. }
  891. pcs->desc.name = DRIVER_NAME;
  892. pcs->desc.pctlops = &pcs_pinctrl_ops;
  893. pcs->desc.pmxops = &pcs_pinmux_ops;
  894. pcs->desc.confops = &pcs_pinconf_ops;
  895. pcs->desc.owner = THIS_MODULE;
  896. ret = pcs_allocate_pin_table(pcs);
  897. if (ret < 0)
  898. goto free;
  899. pcs->pctl = pinctrl_register(&pcs->desc, pcs->dev, pcs);
  900. if (!pcs->pctl) {
  901. dev_err(pcs->dev, "could not register single pinctrl driver\n");
  902. ret = -EINVAL;
  903. goto free;
  904. }
  905. ret = pcs_add_gpio_func(np, pcs);
  906. if (ret < 0)
  907. goto free;
  908. dev_info(pcs->dev, "%i pins at pa %p size %u\n",
  909. pcs->desc.npins, pcs->base, pcs->size);
  910. return 0;
  911. free:
  912. pcs_free_resources(pcs);
  913. return ret;
  914. }
  915. static int pcs_remove(struct platform_device *pdev)
  916. {
  917. struct pcs_device *pcs = platform_get_drvdata(pdev);
  918. if (!pcs)
  919. return 0;
  920. pcs_free_resources(pcs);
  921. return 0;
  922. }
  923. static struct of_device_id pcs_of_match[] = {
  924. { .compatible = DRIVER_NAME, },
  925. { },
  926. };
  927. MODULE_DEVICE_TABLE(of, pcs_of_match);
  928. static struct platform_driver pcs_driver = {
  929. .probe = pcs_probe,
  930. .remove = pcs_remove,
  931. .driver = {
  932. .owner = THIS_MODULE,
  933. .name = DRIVER_NAME,
  934. .of_match_table = pcs_of_match,
  935. },
  936. };
  937. module_platform_driver(pcs_driver);
  938. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  939. MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver");
  940. MODULE_LICENSE("GPL v2");