pinctrl-single.c 24 KB

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