pinctrl-single.c 24 KB

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