pinctrl-single.c 26 KB

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