pinctrl-single.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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 <linux/pinctrl/pinconf-generic.h>
  23. #include "core.h"
  24. #include "pinconf.h"
  25. #define DRIVER_NAME "pinctrl-single"
  26. #define PCS_MUX_PINS_NAME "pinctrl-single,pins"
  27. #define PCS_MUX_BITS_NAME "pinctrl-single,bits"
  28. #define PCS_REG_NAME_LEN ((sizeof(unsigned long) * 2) + 1)
  29. #define PCS_OFF_DISABLED ~0U
  30. /**
  31. * struct pcs_pingroup - pingroups for a function
  32. * @np: pingroup device node pointer
  33. * @name: pingroup name
  34. * @gpins: array of the pins in the group
  35. * @ngpins: number of pins in the group
  36. * @node: list node
  37. */
  38. struct pcs_pingroup {
  39. struct device_node *np;
  40. const char *name;
  41. int *gpins;
  42. int ngpins;
  43. struct list_head node;
  44. };
  45. /**
  46. * struct pcs_func_vals - mux function register offset and value pair
  47. * @reg: register virtual address
  48. * @val: register value
  49. */
  50. struct pcs_func_vals {
  51. void __iomem *reg;
  52. unsigned val;
  53. unsigned mask;
  54. };
  55. /**
  56. * struct pcs_conf_vals - pinconf parameter, pinconf register offset
  57. * and value, enable, disable, mask
  58. * @param: config parameter
  59. * @val: user input bits in the pinconf register
  60. * @enable: enable bits in the pinconf register
  61. * @disable: disable bits in the pinconf register
  62. * @mask: mask bits in the register value
  63. */
  64. struct pcs_conf_vals {
  65. enum pin_config_param param;
  66. unsigned val;
  67. unsigned enable;
  68. unsigned disable;
  69. unsigned mask;
  70. };
  71. /**
  72. * struct pcs_conf_type - pinconf property name, pinconf param pair
  73. * @name: property name in DTS file
  74. * @param: config parameter
  75. */
  76. struct pcs_conf_type {
  77. const char *name;
  78. enum pin_config_param param;
  79. };
  80. /**
  81. * struct pcs_function - pinctrl function
  82. * @name: pinctrl function name
  83. * @vals: register and vals array
  84. * @nvals: number of entries in vals array
  85. * @pgnames: array of pingroup names the function uses
  86. * @npgnames: number of pingroup names the function uses
  87. * @node: list node
  88. */
  89. struct pcs_function {
  90. const char *name;
  91. struct pcs_func_vals *vals;
  92. unsigned nvals;
  93. const char **pgnames;
  94. int npgnames;
  95. struct pcs_conf_vals *conf;
  96. int nconfs;
  97. struct list_head node;
  98. };
  99. /**
  100. * struct pcs_gpiofunc_range - pin ranges with same mux value of gpio function
  101. * @offset: offset base of pins
  102. * @npins: number pins with the same mux value of gpio function
  103. * @gpiofunc: mux value of gpio function
  104. * @node: list node
  105. */
  106. struct pcs_gpiofunc_range {
  107. unsigned offset;
  108. unsigned npins;
  109. unsigned gpiofunc;
  110. struct list_head node;
  111. };
  112. /**
  113. * struct pcs_data - wrapper for data needed by pinctrl framework
  114. * @pa: pindesc array
  115. * @cur: index to current element
  116. *
  117. * REVISIT: We should be able to drop this eventually by adding
  118. * support for registering pins individually in the pinctrl
  119. * framework for those drivers that don't need a static array.
  120. */
  121. struct pcs_data {
  122. struct pinctrl_pin_desc *pa;
  123. int cur;
  124. };
  125. /**
  126. * struct pcs_name - register name for a pin
  127. * @name: name of the pinctrl register
  128. *
  129. * REVISIT: We may want to make names optional in the pinctrl
  130. * framework as some drivers may not care about pin names to
  131. * avoid kernel bloat. The pin names can be deciphered by user
  132. * space tools using debugfs based on the register address and
  133. * SoC packaging information.
  134. */
  135. struct pcs_name {
  136. char name[PCS_REG_NAME_LEN];
  137. };
  138. /**
  139. * struct pcs_device - pinctrl device instance
  140. * @res: resources
  141. * @base: virtual address of the controller
  142. * @size: size of the ioremapped area
  143. * @dev: device entry
  144. * @pctl: pin controller device
  145. * @mutex: mutex protecting the lists
  146. * @width: bits per mux register
  147. * @fmask: function register mask
  148. * @fshift: function register shift
  149. * @foff: value to turn mux off
  150. * @fmax: max number of functions in fmask
  151. * @is_pinconf: whether supports pinconf
  152. * @names: array of register names for pins
  153. * @pins: physical pins on the SoC
  154. * @pgtree: pingroup index radix tree
  155. * @ftree: function index radix tree
  156. * @pingroups: list of pingroups
  157. * @functions: list of functions
  158. * @gpiofuncs: list of gpio functions
  159. * @ngroups: number of pingroups
  160. * @nfuncs: number of functions
  161. * @desc: pin controller descriptor
  162. * @read: register read function to use
  163. * @write: register write function to use
  164. */
  165. struct pcs_device {
  166. struct resource *res;
  167. void __iomem *base;
  168. unsigned size;
  169. struct device *dev;
  170. struct pinctrl_dev *pctl;
  171. struct mutex mutex;
  172. unsigned width;
  173. unsigned fmask;
  174. unsigned fshift;
  175. unsigned foff;
  176. unsigned fmax;
  177. bool bits_per_mux;
  178. bool is_pinconf;
  179. struct pcs_name *names;
  180. struct pcs_data pins;
  181. struct radix_tree_root pgtree;
  182. struct radix_tree_root ftree;
  183. struct list_head pingroups;
  184. struct list_head functions;
  185. struct list_head gpiofuncs;
  186. unsigned ngroups;
  187. unsigned nfuncs;
  188. struct pinctrl_desc desc;
  189. unsigned (*read)(void __iomem *reg);
  190. void (*write)(unsigned val, void __iomem *reg);
  191. };
  192. static int pcs_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
  193. unsigned long *config);
  194. static int pcs_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
  195. unsigned long config);
  196. static enum pin_config_param pcs_bias[] = {
  197. PIN_CONFIG_BIAS_PULL_DOWN,
  198. PIN_CONFIG_BIAS_PULL_UP,
  199. };
  200. /*
  201. * REVISIT: Reads and writes could eventually use regmap or something
  202. * generic. But at least on omaps, some mux registers are performance
  203. * critical as they may need to be remuxed every time before and after
  204. * idle. Adding tests for register access width for every read and
  205. * write like regmap is doing is not desired, and caching the registers
  206. * does not help in this case.
  207. */
  208. static unsigned __maybe_unused pcs_readb(void __iomem *reg)
  209. {
  210. return readb(reg);
  211. }
  212. static unsigned __maybe_unused pcs_readw(void __iomem *reg)
  213. {
  214. return readw(reg);
  215. }
  216. static unsigned __maybe_unused pcs_readl(void __iomem *reg)
  217. {
  218. return readl(reg);
  219. }
  220. static void __maybe_unused pcs_writeb(unsigned val, void __iomem *reg)
  221. {
  222. writeb(val, reg);
  223. }
  224. static void __maybe_unused pcs_writew(unsigned val, void __iomem *reg)
  225. {
  226. writew(val, reg);
  227. }
  228. static void __maybe_unused pcs_writel(unsigned val, void __iomem *reg)
  229. {
  230. writel(val, reg);
  231. }
  232. static int pcs_get_groups_count(struct pinctrl_dev *pctldev)
  233. {
  234. struct pcs_device *pcs;
  235. pcs = pinctrl_dev_get_drvdata(pctldev);
  236. return pcs->ngroups;
  237. }
  238. static const char *pcs_get_group_name(struct pinctrl_dev *pctldev,
  239. unsigned gselector)
  240. {
  241. struct pcs_device *pcs;
  242. struct pcs_pingroup *group;
  243. pcs = pinctrl_dev_get_drvdata(pctldev);
  244. group = radix_tree_lookup(&pcs->pgtree, gselector);
  245. if (!group) {
  246. dev_err(pcs->dev, "%s could not find pingroup%i\n",
  247. __func__, gselector);
  248. return NULL;
  249. }
  250. return group->name;
  251. }
  252. static int pcs_get_group_pins(struct pinctrl_dev *pctldev,
  253. unsigned gselector,
  254. const unsigned **pins,
  255. unsigned *npins)
  256. {
  257. struct pcs_device *pcs;
  258. struct pcs_pingroup *group;
  259. pcs = pinctrl_dev_get_drvdata(pctldev);
  260. group = radix_tree_lookup(&pcs->pgtree, gselector);
  261. if (!group) {
  262. dev_err(pcs->dev, "%s could not find pingroup%i\n",
  263. __func__, gselector);
  264. return -EINVAL;
  265. }
  266. *pins = group->gpins;
  267. *npins = group->ngpins;
  268. return 0;
  269. }
  270. static void pcs_pin_dbg_show(struct pinctrl_dev *pctldev,
  271. struct seq_file *s,
  272. unsigned pin)
  273. {
  274. struct pcs_device *pcs;
  275. unsigned val, mux_bytes;
  276. pcs = pinctrl_dev_get_drvdata(pctldev);
  277. mux_bytes = pcs->width / BITS_PER_BYTE;
  278. val = pcs->read(pcs->base + pin * mux_bytes);
  279. seq_printf(s, "%08x %s " , val, DRIVER_NAME);
  280. }
  281. static void pcs_dt_free_map(struct pinctrl_dev *pctldev,
  282. struct pinctrl_map *map, unsigned num_maps)
  283. {
  284. struct pcs_device *pcs;
  285. pcs = pinctrl_dev_get_drvdata(pctldev);
  286. devm_kfree(pcs->dev, map);
  287. }
  288. static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
  289. struct device_node *np_config,
  290. struct pinctrl_map **map, unsigned *num_maps);
  291. static const struct pinctrl_ops pcs_pinctrl_ops = {
  292. .get_groups_count = pcs_get_groups_count,
  293. .get_group_name = pcs_get_group_name,
  294. .get_group_pins = pcs_get_group_pins,
  295. .pin_dbg_show = pcs_pin_dbg_show,
  296. .dt_node_to_map = pcs_dt_node_to_map,
  297. .dt_free_map = pcs_dt_free_map,
  298. };
  299. static int pcs_get_functions_count(struct pinctrl_dev *pctldev)
  300. {
  301. struct pcs_device *pcs;
  302. pcs = pinctrl_dev_get_drvdata(pctldev);
  303. return pcs->nfuncs;
  304. }
  305. static const char *pcs_get_function_name(struct pinctrl_dev *pctldev,
  306. unsigned fselector)
  307. {
  308. struct pcs_device *pcs;
  309. struct pcs_function *func;
  310. pcs = pinctrl_dev_get_drvdata(pctldev);
  311. func = radix_tree_lookup(&pcs->ftree, fselector);
  312. if (!func) {
  313. dev_err(pcs->dev, "%s could not find function%i\n",
  314. __func__, fselector);
  315. return NULL;
  316. }
  317. return func->name;
  318. }
  319. static int pcs_get_function_groups(struct pinctrl_dev *pctldev,
  320. unsigned fselector,
  321. const char * const **groups,
  322. unsigned * const ngroups)
  323. {
  324. struct pcs_device *pcs;
  325. struct pcs_function *func;
  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 -EINVAL;
  332. }
  333. *groups = func->pgnames;
  334. *ngroups = func->npgnames;
  335. return 0;
  336. }
  337. static int pcs_get_function(struct pinctrl_dev *pctldev, unsigned pin,
  338. struct pcs_function **func)
  339. {
  340. struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
  341. struct pin_desc *pdesc = pin_desc_get(pctldev, pin);
  342. const struct pinctrl_setting_mux *setting;
  343. unsigned fselector;
  344. /* If pin is not described in DTS & enabled, mux_setting is NULL. */
  345. setting = pdesc->mux_setting;
  346. if (!setting)
  347. return -ENOTSUPP;
  348. fselector = setting->func;
  349. *func = radix_tree_lookup(&pcs->ftree, fselector);
  350. if (!(*func)) {
  351. dev_err(pcs->dev, "%s could not find function%i\n",
  352. __func__, fselector);
  353. return -ENOTSUPP;
  354. }
  355. return 0;
  356. }
  357. static int pcs_enable(struct pinctrl_dev *pctldev, unsigned fselector,
  358. unsigned group)
  359. {
  360. struct pcs_device *pcs;
  361. struct pcs_function *func;
  362. int i;
  363. pcs = pinctrl_dev_get_drvdata(pctldev);
  364. /* If function mask is null, needn't enable it. */
  365. if (!pcs->fmask)
  366. return 0;
  367. func = radix_tree_lookup(&pcs->ftree, fselector);
  368. if (!func)
  369. return -EINVAL;
  370. dev_dbg(pcs->dev, "enabling %s function%i\n",
  371. func->name, fselector);
  372. for (i = 0; i < func->nvals; i++) {
  373. struct pcs_func_vals *vals;
  374. unsigned val, mask;
  375. vals = &func->vals[i];
  376. val = pcs->read(vals->reg);
  377. if (!vals->mask)
  378. mask = pcs->fmask;
  379. else
  380. mask = pcs->fmask & vals->mask;
  381. val &= ~mask;
  382. val |= (vals->val & mask);
  383. pcs->write(val, vals->reg);
  384. }
  385. return 0;
  386. }
  387. static void pcs_disable(struct pinctrl_dev *pctldev, unsigned fselector,
  388. unsigned group)
  389. {
  390. struct pcs_device *pcs;
  391. struct pcs_function *func;
  392. int i;
  393. pcs = pinctrl_dev_get_drvdata(pctldev);
  394. /* If function mask is null, needn't disable it. */
  395. if (!pcs->fmask)
  396. return;
  397. func = radix_tree_lookup(&pcs->ftree, fselector);
  398. if (!func) {
  399. dev_err(pcs->dev, "%s could not find function%i\n",
  400. __func__, fselector);
  401. return;
  402. }
  403. /*
  404. * Ignore disable if function-off is not specified. Some hardware
  405. * does not have clearly defined disable function. For pin specific
  406. * off modes, you can use alternate named states as described in
  407. * pinctrl-bindings.txt.
  408. */
  409. if (pcs->foff == PCS_OFF_DISABLED) {
  410. dev_dbg(pcs->dev, "ignoring disable for %s function%i\n",
  411. func->name, fselector);
  412. return;
  413. }
  414. dev_dbg(pcs->dev, "disabling function%i %s\n",
  415. fselector, func->name);
  416. for (i = 0; i < func->nvals; i++) {
  417. struct pcs_func_vals *vals;
  418. unsigned val;
  419. vals = &func->vals[i];
  420. val = pcs->read(vals->reg);
  421. val &= ~pcs->fmask;
  422. val |= pcs->foff << pcs->fshift;
  423. pcs->write(val, vals->reg);
  424. }
  425. }
  426. static int pcs_request_gpio(struct pinctrl_dev *pctldev,
  427. struct pinctrl_gpio_range *range, unsigned pin)
  428. {
  429. struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
  430. struct pcs_gpiofunc_range *frange = NULL;
  431. struct list_head *pos, *tmp;
  432. int mux_bytes = 0;
  433. unsigned data;
  434. /* If function mask is null, return directly. */
  435. if (!pcs->fmask)
  436. return -ENOTSUPP;
  437. list_for_each_safe(pos, tmp, &pcs->gpiofuncs) {
  438. frange = list_entry(pos, struct pcs_gpiofunc_range, node);
  439. if (pin >= frange->offset + frange->npins
  440. || pin < frange->offset)
  441. continue;
  442. mux_bytes = pcs->width / BITS_PER_BYTE;
  443. data = pcs->read(pcs->base + pin * mux_bytes) & ~pcs->fmask;
  444. data |= frange->gpiofunc;
  445. pcs->write(data, pcs->base + pin * mux_bytes);
  446. break;
  447. }
  448. return 0;
  449. }
  450. static const struct pinmux_ops pcs_pinmux_ops = {
  451. .get_functions_count = pcs_get_functions_count,
  452. .get_function_name = pcs_get_function_name,
  453. .get_function_groups = pcs_get_function_groups,
  454. .enable = pcs_enable,
  455. .disable = pcs_disable,
  456. .gpio_request_enable = pcs_request_gpio,
  457. };
  458. /* Clear BIAS value */
  459. static void pcs_pinconf_clear_bias(struct pinctrl_dev *pctldev, unsigned pin)
  460. {
  461. unsigned long config;
  462. int i;
  463. for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) {
  464. config = pinconf_to_config_packed(pcs_bias[i], 0);
  465. pcs_pinconf_set(pctldev, pin, config);
  466. }
  467. }
  468. /*
  469. * Check whether PIN_CONFIG_BIAS_DISABLE is valid.
  470. * It's depend on that PULL_DOWN & PULL_UP configs are all invalid.
  471. */
  472. static bool pcs_pinconf_bias_disable(struct pinctrl_dev *pctldev, unsigned pin)
  473. {
  474. unsigned long config;
  475. int i;
  476. for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) {
  477. config = pinconf_to_config_packed(pcs_bias[i], 0);
  478. if (!pcs_pinconf_get(pctldev, pin, &config))
  479. goto out;
  480. }
  481. return true;
  482. out:
  483. return false;
  484. }
  485. static int pcs_pinconf_get(struct pinctrl_dev *pctldev,
  486. unsigned pin, unsigned long *config)
  487. {
  488. struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
  489. struct pcs_function *func;
  490. enum pin_config_param param;
  491. unsigned offset = 0, data = 0, i, j, ret;
  492. ret = pcs_get_function(pctldev, pin, &func);
  493. if (ret)
  494. return ret;
  495. for (i = 0; i < func->nconfs; i++) {
  496. param = pinconf_to_config_param(*config);
  497. if (param == PIN_CONFIG_BIAS_DISABLE) {
  498. if (pcs_pinconf_bias_disable(pctldev, pin)) {
  499. *config = 0;
  500. return 0;
  501. } else {
  502. return -ENOTSUPP;
  503. }
  504. } else if (param != func->conf[i].param) {
  505. continue;
  506. }
  507. offset = pin * (pcs->width / BITS_PER_BYTE);
  508. data = pcs->read(pcs->base + offset) & func->conf[i].mask;
  509. switch (func->conf[i].param) {
  510. /* 4 parameters */
  511. case PIN_CONFIG_BIAS_PULL_DOWN:
  512. case PIN_CONFIG_BIAS_PULL_UP:
  513. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  514. if ((data != func->conf[i].enable) ||
  515. (data == func->conf[i].disable))
  516. return -ENOTSUPP;
  517. *config = 0;
  518. break;
  519. /* 2 parameters */
  520. case PIN_CONFIG_INPUT_SCHMITT:
  521. for (j = 0; j < func->nconfs; j++) {
  522. switch (func->conf[j].param) {
  523. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  524. if (data != func->conf[j].enable)
  525. return -ENOTSUPP;
  526. break;
  527. default:
  528. break;
  529. }
  530. }
  531. *config = data;
  532. break;
  533. case PIN_CONFIG_DRIVE_STRENGTH:
  534. case PIN_CONFIG_SLEW_RATE:
  535. default:
  536. *config = data;
  537. break;
  538. }
  539. return 0;
  540. }
  541. return -ENOTSUPP;
  542. }
  543. static int pcs_pinconf_set(struct pinctrl_dev *pctldev,
  544. unsigned pin, unsigned long config)
  545. {
  546. struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
  547. struct pcs_function *func;
  548. unsigned offset = 0, shift = 0, arg = 0, i, data, ret;
  549. u16 argument;
  550. ret = pcs_get_function(pctldev, pin, &func);
  551. if (ret)
  552. return ret;
  553. for (i = 0; i < func->nconfs; i++) {
  554. if (pinconf_to_config_param(config) == func->conf[i].param) {
  555. offset = pin * (pcs->width / BITS_PER_BYTE);
  556. data = pcs->read(pcs->base + offset);
  557. argument = pinconf_to_config_argument(config);
  558. switch (func->conf[i].param) {
  559. /* 2 parameters */
  560. case PIN_CONFIG_INPUT_SCHMITT:
  561. case PIN_CONFIG_DRIVE_STRENGTH:
  562. case PIN_CONFIG_SLEW_RATE:
  563. shift = ffs(func->conf[i].mask) - 1;
  564. arg = pinconf_to_config_argument(config);
  565. data &= ~func->conf[i].mask;
  566. data |= (arg << shift) & func->conf[i].mask;
  567. break;
  568. /* 4 parameters */
  569. case PIN_CONFIG_BIAS_DISABLE:
  570. pcs_pinconf_clear_bias(pctldev, pin);
  571. break;
  572. case PIN_CONFIG_BIAS_PULL_DOWN:
  573. case PIN_CONFIG_BIAS_PULL_UP:
  574. if (argument)
  575. pcs_pinconf_clear_bias(pctldev, pin);
  576. /* fall through */
  577. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  578. data &= ~func->conf[i].mask;
  579. if (argument)
  580. data |= func->conf[i].enable;
  581. else
  582. data |= func->conf[i].disable;
  583. break;
  584. default:
  585. return -ENOTSUPP;
  586. }
  587. pcs->write(data, pcs->base + offset);
  588. return 0;
  589. }
  590. }
  591. return -ENOTSUPP;
  592. }
  593. static int pcs_pinconf_group_get(struct pinctrl_dev *pctldev,
  594. unsigned group, unsigned long *config)
  595. {
  596. const unsigned *pins;
  597. unsigned npins, old = 0;
  598. int i, ret;
  599. ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
  600. if (ret)
  601. return ret;
  602. for (i = 0; i < npins; i++) {
  603. if (pcs_pinconf_get(pctldev, pins[i], config))
  604. return -ENOTSUPP;
  605. /* configs do not match between two pins */
  606. if (i && (old != *config))
  607. return -ENOTSUPP;
  608. old = *config;
  609. }
  610. return 0;
  611. }
  612. static int pcs_pinconf_group_set(struct pinctrl_dev *pctldev,
  613. unsigned group, unsigned long config)
  614. {
  615. const unsigned *pins;
  616. unsigned npins;
  617. int i, ret;
  618. ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
  619. if (ret)
  620. return ret;
  621. for (i = 0; i < npins; i++) {
  622. if (pcs_pinconf_set(pctldev, pins[i], config))
  623. return -ENOTSUPP;
  624. }
  625. return 0;
  626. }
  627. static void pcs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  628. struct seq_file *s, unsigned pin)
  629. {
  630. }
  631. static void pcs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  632. struct seq_file *s, unsigned selector)
  633. {
  634. }
  635. static void pcs_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
  636. struct seq_file *s,
  637. unsigned long config)
  638. {
  639. pinconf_generic_dump_config(pctldev, s, config);
  640. }
  641. static const struct pinconf_ops pcs_pinconf_ops = {
  642. .pin_config_get = pcs_pinconf_get,
  643. .pin_config_set = pcs_pinconf_set,
  644. .pin_config_group_get = pcs_pinconf_group_get,
  645. .pin_config_group_set = pcs_pinconf_group_set,
  646. .pin_config_dbg_show = pcs_pinconf_dbg_show,
  647. .pin_config_group_dbg_show = pcs_pinconf_group_dbg_show,
  648. .pin_config_config_dbg_show = pcs_pinconf_config_dbg_show,
  649. };
  650. /**
  651. * pcs_add_pin() - add a pin to the static per controller pin array
  652. * @pcs: pcs driver instance
  653. * @offset: register offset from base
  654. */
  655. static int pcs_add_pin(struct pcs_device *pcs, unsigned offset)
  656. {
  657. struct pinctrl_pin_desc *pin;
  658. struct pcs_name *pn;
  659. int i;
  660. i = pcs->pins.cur;
  661. if (i >= pcs->desc.npins) {
  662. dev_err(pcs->dev, "too many pins, max %i\n",
  663. pcs->desc.npins);
  664. return -ENOMEM;
  665. }
  666. pin = &pcs->pins.pa[i];
  667. pn = &pcs->names[i];
  668. sprintf(pn->name, "%lx",
  669. (unsigned long)pcs->res->start + offset);
  670. pin->name = pn->name;
  671. pin->number = i;
  672. pcs->pins.cur++;
  673. return i;
  674. }
  675. /**
  676. * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver
  677. * @pcs: pcs driver instance
  678. *
  679. * In case of errors, resources are freed in pcs_free_resources.
  680. *
  681. * If your hardware needs holes in the address space, then just set
  682. * up multiple driver instances.
  683. */
  684. static int pcs_allocate_pin_table(struct pcs_device *pcs)
  685. {
  686. int mux_bytes, nr_pins, i;
  687. mux_bytes = pcs->width / BITS_PER_BYTE;
  688. nr_pins = pcs->size / mux_bytes;
  689. dev_dbg(pcs->dev, "allocating %i pins\n", nr_pins);
  690. pcs->pins.pa = devm_kzalloc(pcs->dev,
  691. sizeof(*pcs->pins.pa) * nr_pins,
  692. GFP_KERNEL);
  693. if (!pcs->pins.pa)
  694. return -ENOMEM;
  695. pcs->names = devm_kzalloc(pcs->dev,
  696. sizeof(struct pcs_name) * nr_pins,
  697. GFP_KERNEL);
  698. if (!pcs->names)
  699. return -ENOMEM;
  700. pcs->desc.pins = pcs->pins.pa;
  701. pcs->desc.npins = nr_pins;
  702. for (i = 0; i < pcs->desc.npins; i++) {
  703. unsigned offset;
  704. int res;
  705. offset = i * mux_bytes;
  706. res = pcs_add_pin(pcs, offset);
  707. if (res < 0) {
  708. dev_err(pcs->dev, "error adding pins: %i\n", res);
  709. return res;
  710. }
  711. }
  712. return 0;
  713. }
  714. /**
  715. * pcs_add_function() - adds a new function to the function list
  716. * @pcs: pcs driver instance
  717. * @np: device node of the mux entry
  718. * @name: name of the function
  719. * @vals: array of mux register value pairs used by the function
  720. * @nvals: number of mux register value pairs
  721. * @pgnames: array of pingroup names for the function
  722. * @npgnames: number of pingroup names
  723. */
  724. static struct pcs_function *pcs_add_function(struct pcs_device *pcs,
  725. struct device_node *np,
  726. const char *name,
  727. struct pcs_func_vals *vals,
  728. unsigned nvals,
  729. const char **pgnames,
  730. unsigned npgnames)
  731. {
  732. struct pcs_function *function;
  733. function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL);
  734. if (!function)
  735. return NULL;
  736. function->name = name;
  737. function->vals = vals;
  738. function->nvals = nvals;
  739. function->pgnames = pgnames;
  740. function->npgnames = npgnames;
  741. mutex_lock(&pcs->mutex);
  742. list_add_tail(&function->node, &pcs->functions);
  743. radix_tree_insert(&pcs->ftree, pcs->nfuncs, function);
  744. pcs->nfuncs++;
  745. mutex_unlock(&pcs->mutex);
  746. return function;
  747. }
  748. static void pcs_remove_function(struct pcs_device *pcs,
  749. struct pcs_function *function)
  750. {
  751. int i;
  752. mutex_lock(&pcs->mutex);
  753. for (i = 0; i < pcs->nfuncs; i++) {
  754. struct pcs_function *found;
  755. found = radix_tree_lookup(&pcs->ftree, i);
  756. if (found == function)
  757. radix_tree_delete(&pcs->ftree, i);
  758. }
  759. list_del(&function->node);
  760. mutex_unlock(&pcs->mutex);
  761. }
  762. /**
  763. * pcs_add_pingroup() - add a pingroup to the pingroup list
  764. * @pcs: pcs driver instance
  765. * @np: device node of the mux entry
  766. * @name: name of the pingroup
  767. * @gpins: array of the pins that belong to the group
  768. * @ngpins: number of pins in the group
  769. */
  770. static int pcs_add_pingroup(struct pcs_device *pcs,
  771. struct device_node *np,
  772. const char *name,
  773. int *gpins,
  774. int ngpins)
  775. {
  776. struct pcs_pingroup *pingroup;
  777. pingroup = devm_kzalloc(pcs->dev, sizeof(*pingroup), GFP_KERNEL);
  778. if (!pingroup)
  779. return -ENOMEM;
  780. pingroup->name = name;
  781. pingroup->np = np;
  782. pingroup->gpins = gpins;
  783. pingroup->ngpins = ngpins;
  784. mutex_lock(&pcs->mutex);
  785. list_add_tail(&pingroup->node, &pcs->pingroups);
  786. radix_tree_insert(&pcs->pgtree, pcs->ngroups, pingroup);
  787. pcs->ngroups++;
  788. mutex_unlock(&pcs->mutex);
  789. return 0;
  790. }
  791. /**
  792. * pcs_get_pin_by_offset() - get a pin index based on the register offset
  793. * @pcs: pcs driver instance
  794. * @offset: register offset from the base
  795. *
  796. * Note that this is OK as long as the pins are in a static array.
  797. */
  798. static int pcs_get_pin_by_offset(struct pcs_device *pcs, unsigned offset)
  799. {
  800. unsigned index;
  801. if (offset >= pcs->size) {
  802. dev_err(pcs->dev, "mux offset out of range: 0x%x (0x%x)\n",
  803. offset, pcs->size);
  804. return -EINVAL;
  805. }
  806. index = offset / (pcs->width / BITS_PER_BYTE);
  807. return index;
  808. }
  809. /*
  810. * check whether data matches enable bits or disable bits
  811. * Return value: 1 for matching enable bits, 0 for matching disable bits,
  812. * and negative value for matching failure.
  813. */
  814. static int pcs_config_match(unsigned data, unsigned enable, unsigned disable)
  815. {
  816. int ret = -EINVAL;
  817. if (data == enable)
  818. ret = 1;
  819. else if (data == disable)
  820. ret = 0;
  821. return ret;
  822. }
  823. static void add_config(struct pcs_conf_vals **conf, enum pin_config_param param,
  824. unsigned value, unsigned enable, unsigned disable,
  825. unsigned mask)
  826. {
  827. (*conf)->param = param;
  828. (*conf)->val = value;
  829. (*conf)->enable = enable;
  830. (*conf)->disable = disable;
  831. (*conf)->mask = mask;
  832. (*conf)++;
  833. }
  834. static void add_setting(unsigned long **setting, enum pin_config_param param,
  835. unsigned arg)
  836. {
  837. **setting = pinconf_to_config_packed(param, arg);
  838. (*setting)++;
  839. }
  840. /* add pinconf setting with 2 parameters */
  841. static void pcs_add_conf2(struct pcs_device *pcs, struct device_node *np,
  842. const char *name, enum pin_config_param param,
  843. struct pcs_conf_vals **conf, unsigned long **settings)
  844. {
  845. unsigned value[2];
  846. int ret;
  847. ret = of_property_read_u32_array(np, name, value, 2);
  848. if (ret)
  849. return;
  850. /* set value & mask */
  851. value[0] &= value[1];
  852. /* skip enable & disable */
  853. add_config(conf, param, value[0], 0, 0, value[1]);
  854. add_setting(settings, param, value[0]);
  855. }
  856. /* add pinconf setting with 4 parameters */
  857. static void pcs_add_conf4(struct pcs_device *pcs, struct device_node *np,
  858. const char *name, enum pin_config_param param,
  859. struct pcs_conf_vals **conf, unsigned long **settings)
  860. {
  861. unsigned value[4];
  862. int ret;
  863. /* value to set, enable, disable, mask */
  864. ret = of_property_read_u32_array(np, name, value, 4);
  865. if (ret)
  866. return;
  867. if (!value[3]) {
  868. dev_err(pcs->dev, "mask field of the property can't be 0\n");
  869. return;
  870. }
  871. value[0] &= value[3];
  872. value[1] &= value[3];
  873. value[2] &= value[3];
  874. ret = pcs_config_match(value[0], value[1], value[2]);
  875. if (ret < 0)
  876. dev_dbg(pcs->dev, "failed to match enable or disable bits\n");
  877. add_config(conf, param, value[0], value[1], value[2], value[3]);
  878. add_setting(settings, param, ret);
  879. }
  880. static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np,
  881. struct pcs_function *func,
  882. struct pinctrl_map **map)
  883. {
  884. struct pinctrl_map *m = *map;
  885. int i = 0, nconfs = 0;
  886. unsigned long *settings = NULL, *s = NULL;
  887. struct pcs_conf_vals *conf = NULL;
  888. struct pcs_conf_type prop2[] = {
  889. { "pinctrl-single,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, },
  890. { "pinctrl-single,slew-rate", PIN_CONFIG_SLEW_RATE, },
  891. { "pinctrl-single,input-schmitt", PIN_CONFIG_INPUT_SCHMITT, },
  892. };
  893. struct pcs_conf_type prop4[] = {
  894. { "pinctrl-single,bias-pullup", PIN_CONFIG_BIAS_PULL_UP, },
  895. { "pinctrl-single,bias-pulldown", PIN_CONFIG_BIAS_PULL_DOWN, },
  896. { "pinctrl-single,input-schmitt-enable",
  897. PIN_CONFIG_INPUT_SCHMITT_ENABLE, },
  898. };
  899. /* If pinconf isn't supported, don't parse properties in below. */
  900. if (!pcs->is_pinconf)
  901. return 0;
  902. /* cacluate how much properties are supported in current node */
  903. for (i = 0; i < ARRAY_SIZE(prop2); i++) {
  904. if (of_find_property(np, prop2[i].name, NULL))
  905. nconfs++;
  906. }
  907. for (i = 0; i < ARRAY_SIZE(prop4); i++) {
  908. if (of_find_property(np, prop4[i].name, NULL))
  909. nconfs++;
  910. }
  911. if (!nconfs)
  912. return 0;
  913. func->conf = devm_kzalloc(pcs->dev,
  914. sizeof(struct pcs_conf_vals) * nconfs,
  915. GFP_KERNEL);
  916. if (!func->conf)
  917. return -ENOMEM;
  918. func->nconfs = nconfs;
  919. conf = &(func->conf[0]);
  920. m++;
  921. settings = devm_kzalloc(pcs->dev, sizeof(unsigned long) * nconfs,
  922. GFP_KERNEL);
  923. if (!settings)
  924. return -ENOMEM;
  925. s = &settings[0];
  926. for (i = 0; i < ARRAY_SIZE(prop2); i++)
  927. pcs_add_conf2(pcs, np, prop2[i].name, prop2[i].param,
  928. &conf, &s);
  929. for (i = 0; i < ARRAY_SIZE(prop4); i++)
  930. pcs_add_conf4(pcs, np, prop4[i].name, prop4[i].param,
  931. &conf, &s);
  932. m->type = PIN_MAP_TYPE_CONFIGS_GROUP;
  933. m->data.configs.group_or_pin = np->name;
  934. m->data.configs.configs = settings;
  935. m->data.configs.num_configs = nconfs;
  936. return 0;
  937. }
  938. static void pcs_free_pingroups(struct pcs_device *pcs);
  939. /**
  940. * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
  941. * @pcs: pinctrl driver instance
  942. * @np: device node of the mux entry
  943. * @map: map entry
  944. * @num_maps: number of map
  945. * @pgnames: pingroup names
  946. *
  947. * Note that this binding currently supports only sets of one register + value.
  948. *
  949. * Also note that this driver tries to avoid understanding pin and function
  950. * names because of the extra bloat they would cause especially in the case of
  951. * a large number of pins. This driver just sets what is specified for the board
  952. * in the .dts file. Further user space debugging tools can be developed to
  953. * decipher the pin and function names using debugfs.
  954. *
  955. * If you are concerned about the boot time, set up the static pins in
  956. * the bootloader, and only set up selected pins as device tree entries.
  957. */
  958. static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
  959. struct device_node *np,
  960. struct pinctrl_map **map,
  961. unsigned *num_maps,
  962. const char **pgnames)
  963. {
  964. struct pcs_func_vals *vals;
  965. const __be32 *mux;
  966. int size, params, rows, *pins, index = 0, found = 0, res = -ENOMEM;
  967. struct pcs_function *function;
  968. if (pcs->bits_per_mux) {
  969. params = 3;
  970. mux = of_get_property(np, PCS_MUX_BITS_NAME, &size);
  971. } else {
  972. params = 2;
  973. mux = of_get_property(np, PCS_MUX_PINS_NAME, &size);
  974. }
  975. if (!mux) {
  976. dev_err(pcs->dev, "no valid property for %s\n", np->name);
  977. return -EINVAL;
  978. }
  979. if (size < (sizeof(*mux) * params)) {
  980. dev_err(pcs->dev, "bad data for %s\n", np->name);
  981. return -EINVAL;
  982. }
  983. size /= sizeof(*mux); /* Number of elements in array */
  984. rows = size / params;
  985. vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL);
  986. if (!vals)
  987. return -ENOMEM;
  988. pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL);
  989. if (!pins)
  990. goto free_vals;
  991. while (index < size) {
  992. unsigned offset, val;
  993. int pin;
  994. offset = be32_to_cpup(mux + index++);
  995. val = be32_to_cpup(mux + index++);
  996. vals[found].reg = pcs->base + offset;
  997. vals[found].val = val;
  998. if (params == 3) {
  999. val = be32_to_cpup(mux + index++);
  1000. vals[found].mask = val;
  1001. }
  1002. pin = pcs_get_pin_by_offset(pcs, offset);
  1003. if (pin < 0) {
  1004. dev_err(pcs->dev,
  1005. "could not add functions for %s %ux\n",
  1006. np->name, offset);
  1007. break;
  1008. }
  1009. pins[found++] = pin;
  1010. }
  1011. pgnames[0] = np->name;
  1012. function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
  1013. if (!function)
  1014. goto free_pins;
  1015. res = pcs_add_pingroup(pcs, np, np->name, pins, found);
  1016. if (res < 0)
  1017. goto free_function;
  1018. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  1019. (*map)->data.mux.group = np->name;
  1020. (*map)->data.mux.function = np->name;
  1021. if (pcs->is_pinconf) {
  1022. if (pcs_parse_pinconf(pcs, np, function, map))
  1023. goto free_pingroups;
  1024. *num_maps = 2;
  1025. } else {
  1026. *num_maps = 1;
  1027. }
  1028. return 0;
  1029. free_pingroups:
  1030. pcs_free_pingroups(pcs);
  1031. *num_maps = 1;
  1032. free_function:
  1033. pcs_remove_function(pcs, function);
  1034. free_pins:
  1035. devm_kfree(pcs->dev, pins);
  1036. free_vals:
  1037. devm_kfree(pcs->dev, vals);
  1038. return res;
  1039. }
  1040. /**
  1041. * pcs_dt_node_to_map() - allocates and parses pinctrl maps
  1042. * @pctldev: pinctrl instance
  1043. * @np_config: device tree pinmux entry
  1044. * @map: array of map entries
  1045. * @num_maps: number of maps
  1046. */
  1047. static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
  1048. struct device_node *np_config,
  1049. struct pinctrl_map **map, unsigned *num_maps)
  1050. {
  1051. struct pcs_device *pcs;
  1052. const char **pgnames;
  1053. int ret;
  1054. pcs = pinctrl_dev_get_drvdata(pctldev);
  1055. /* create 2 maps. One is for pinmux, and the other is for pinconf. */
  1056. *map = devm_kzalloc(pcs->dev, sizeof(**map) * 2, GFP_KERNEL);
  1057. if (!*map)
  1058. return -ENOMEM;
  1059. *num_maps = 0;
  1060. pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL);
  1061. if (!pgnames) {
  1062. ret = -ENOMEM;
  1063. goto free_map;
  1064. }
  1065. ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map, num_maps,
  1066. pgnames);
  1067. if (ret < 0) {
  1068. dev_err(pcs->dev, "no pins entries for %s\n",
  1069. np_config->name);
  1070. goto free_pgnames;
  1071. }
  1072. return 0;
  1073. free_pgnames:
  1074. devm_kfree(pcs->dev, pgnames);
  1075. free_map:
  1076. devm_kfree(pcs->dev, *map);
  1077. return ret;
  1078. }
  1079. /**
  1080. * pcs_free_funcs() - free memory used by functions
  1081. * @pcs: pcs driver instance
  1082. */
  1083. static void pcs_free_funcs(struct pcs_device *pcs)
  1084. {
  1085. struct list_head *pos, *tmp;
  1086. int i;
  1087. mutex_lock(&pcs->mutex);
  1088. for (i = 0; i < pcs->nfuncs; i++) {
  1089. struct pcs_function *func;
  1090. func = radix_tree_lookup(&pcs->ftree, i);
  1091. if (!func)
  1092. continue;
  1093. radix_tree_delete(&pcs->ftree, i);
  1094. }
  1095. list_for_each_safe(pos, tmp, &pcs->functions) {
  1096. struct pcs_function *function;
  1097. function = list_entry(pos, struct pcs_function, node);
  1098. list_del(&function->node);
  1099. }
  1100. mutex_unlock(&pcs->mutex);
  1101. }
  1102. /**
  1103. * pcs_free_pingroups() - free memory used by pingroups
  1104. * @pcs: pcs driver instance
  1105. */
  1106. static void pcs_free_pingroups(struct pcs_device *pcs)
  1107. {
  1108. struct list_head *pos, *tmp;
  1109. int i;
  1110. mutex_lock(&pcs->mutex);
  1111. for (i = 0; i < pcs->ngroups; i++) {
  1112. struct pcs_pingroup *pingroup;
  1113. pingroup = radix_tree_lookup(&pcs->pgtree, i);
  1114. if (!pingroup)
  1115. continue;
  1116. radix_tree_delete(&pcs->pgtree, i);
  1117. }
  1118. list_for_each_safe(pos, tmp, &pcs->pingroups) {
  1119. struct pcs_pingroup *pingroup;
  1120. pingroup = list_entry(pos, struct pcs_pingroup, node);
  1121. list_del(&pingroup->node);
  1122. }
  1123. mutex_unlock(&pcs->mutex);
  1124. }
  1125. /**
  1126. * pcs_free_resources() - free memory used by this driver
  1127. * @pcs: pcs driver instance
  1128. */
  1129. static void pcs_free_resources(struct pcs_device *pcs)
  1130. {
  1131. if (pcs->pctl)
  1132. pinctrl_unregister(pcs->pctl);
  1133. pcs_free_funcs(pcs);
  1134. pcs_free_pingroups(pcs);
  1135. }
  1136. #define PCS_GET_PROP_U32(name, reg, err) \
  1137. do { \
  1138. ret = of_property_read_u32(np, name, reg); \
  1139. if (ret) { \
  1140. dev_err(pcs->dev, err); \
  1141. return ret; \
  1142. } \
  1143. } while (0);
  1144. static struct of_device_id pcs_of_match[];
  1145. static int pcs_add_gpio_func(struct device_node *node, struct pcs_device *pcs)
  1146. {
  1147. const char *propname = "pinctrl-single,gpio-range";
  1148. const char *cellname = "#pinctrl-single,gpio-range-cells";
  1149. struct of_phandle_args gpiospec;
  1150. struct pcs_gpiofunc_range *range;
  1151. int ret, i;
  1152. for (i = 0; ; i++) {
  1153. ret = of_parse_phandle_with_args(node, propname, cellname,
  1154. i, &gpiospec);
  1155. /* Do not treat it as error. Only treat it as end condition. */
  1156. if (ret) {
  1157. ret = 0;
  1158. break;
  1159. }
  1160. range = devm_kzalloc(pcs->dev, sizeof(*range), GFP_KERNEL);
  1161. if (!range) {
  1162. ret = -ENOMEM;
  1163. break;
  1164. }
  1165. range->offset = gpiospec.args[0];
  1166. range->npins = gpiospec.args[1];
  1167. range->gpiofunc = gpiospec.args[2];
  1168. mutex_lock(&pcs->mutex);
  1169. list_add_tail(&range->node, &pcs->gpiofuncs);
  1170. mutex_unlock(&pcs->mutex);
  1171. }
  1172. return ret;
  1173. }
  1174. static int pcs_probe(struct platform_device *pdev)
  1175. {
  1176. struct device_node *np = pdev->dev.of_node;
  1177. const struct of_device_id *match;
  1178. struct resource *res;
  1179. struct pcs_device *pcs;
  1180. int ret;
  1181. match = of_match_device(pcs_of_match, &pdev->dev);
  1182. if (!match)
  1183. return -EINVAL;
  1184. pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL);
  1185. if (!pcs) {
  1186. dev_err(&pdev->dev, "could not allocate\n");
  1187. return -ENOMEM;
  1188. }
  1189. pcs->dev = &pdev->dev;
  1190. mutex_init(&pcs->mutex);
  1191. INIT_LIST_HEAD(&pcs->pingroups);
  1192. INIT_LIST_HEAD(&pcs->functions);
  1193. INIT_LIST_HEAD(&pcs->gpiofuncs);
  1194. pcs->is_pinconf = match->data;
  1195. PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs->width,
  1196. "register width not specified\n");
  1197. ret = of_property_read_u32(np, "pinctrl-single,function-mask",
  1198. &pcs->fmask);
  1199. if (!ret) {
  1200. pcs->fshift = ffs(pcs->fmask) - 1;
  1201. pcs->fmax = pcs->fmask >> pcs->fshift;
  1202. } else {
  1203. /* If mask property doesn't exist, function mux is invalid. */
  1204. pcs->fmask = 0;
  1205. pcs->fshift = 0;
  1206. pcs->fmax = 0;
  1207. }
  1208. ret = of_property_read_u32(np, "pinctrl-single,function-off",
  1209. &pcs->foff);
  1210. if (ret)
  1211. pcs->foff = PCS_OFF_DISABLED;
  1212. pcs->bits_per_mux = of_property_read_bool(np,
  1213. "pinctrl-single,bit-per-mux");
  1214. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1215. if (!res) {
  1216. dev_err(pcs->dev, "could not get resource\n");
  1217. return -ENODEV;
  1218. }
  1219. pcs->res = devm_request_mem_region(pcs->dev, res->start,
  1220. resource_size(res), DRIVER_NAME);
  1221. if (!pcs->res) {
  1222. dev_err(pcs->dev, "could not get mem_region\n");
  1223. return -EBUSY;
  1224. }
  1225. pcs->size = resource_size(pcs->res);
  1226. pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size);
  1227. if (!pcs->base) {
  1228. dev_err(pcs->dev, "could not ioremap\n");
  1229. return -ENODEV;
  1230. }
  1231. INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL);
  1232. INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL);
  1233. platform_set_drvdata(pdev, pcs);
  1234. switch (pcs->width) {
  1235. case 8:
  1236. pcs->read = pcs_readb;
  1237. pcs->write = pcs_writeb;
  1238. break;
  1239. case 16:
  1240. pcs->read = pcs_readw;
  1241. pcs->write = pcs_writew;
  1242. break;
  1243. case 32:
  1244. pcs->read = pcs_readl;
  1245. pcs->write = pcs_writel;
  1246. break;
  1247. default:
  1248. break;
  1249. }
  1250. pcs->desc.name = DRIVER_NAME;
  1251. pcs->desc.pctlops = &pcs_pinctrl_ops;
  1252. pcs->desc.pmxops = &pcs_pinmux_ops;
  1253. pcs->desc.confops = &pcs_pinconf_ops;
  1254. pcs->desc.owner = THIS_MODULE;
  1255. if (match->data)
  1256. pcs_pinconf_ops.is_generic = true;
  1257. ret = pcs_allocate_pin_table(pcs);
  1258. if (ret < 0)
  1259. goto free;
  1260. pcs->pctl = pinctrl_register(&pcs->desc, pcs->dev, pcs);
  1261. if (!pcs->pctl) {
  1262. dev_err(pcs->dev, "could not register single pinctrl driver\n");
  1263. ret = -EINVAL;
  1264. goto free;
  1265. }
  1266. ret = pcs_add_gpio_func(np, pcs);
  1267. if (ret < 0)
  1268. goto free;
  1269. dev_info(pcs->dev, "%i pins at pa %p size %u\n",
  1270. pcs->desc.npins, pcs->base, pcs->size);
  1271. return 0;
  1272. free:
  1273. pcs_free_resources(pcs);
  1274. return ret;
  1275. }
  1276. static int pcs_remove(struct platform_device *pdev)
  1277. {
  1278. struct pcs_device *pcs = platform_get_drvdata(pdev);
  1279. if (!pcs)
  1280. return 0;
  1281. pcs_free_resources(pcs);
  1282. return 0;
  1283. }
  1284. static struct of_device_id pcs_of_match[] = {
  1285. { .compatible = "pinctrl-single", .data = (void *)false },
  1286. { .compatible = "pinconf-single", .data = (void *)true },
  1287. { },
  1288. };
  1289. MODULE_DEVICE_TABLE(of, pcs_of_match);
  1290. static struct platform_driver pcs_driver = {
  1291. .probe = pcs_probe,
  1292. .remove = pcs_remove,
  1293. .driver = {
  1294. .owner = THIS_MODULE,
  1295. .name = DRIVER_NAME,
  1296. .of_match_table = pcs_of_match,
  1297. },
  1298. };
  1299. module_platform_driver(pcs_driver);
  1300. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  1301. MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver");
  1302. MODULE_LICENSE("GPL v2");