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, i, data, ret;
  549. u16 arg;
  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. arg = 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. data &= ~func->conf[i].mask;
  565. data |= (arg << shift) & func->conf[i].mask;
  566. break;
  567. /* 4 parameters */
  568. case PIN_CONFIG_BIAS_DISABLE:
  569. pcs_pinconf_clear_bias(pctldev, pin);
  570. break;
  571. case PIN_CONFIG_BIAS_PULL_DOWN:
  572. case PIN_CONFIG_BIAS_PULL_UP:
  573. if (arg)
  574. pcs_pinconf_clear_bias(pctldev, pin);
  575. /* fall through */
  576. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  577. data &= ~func->conf[i].mask;
  578. if (arg)
  579. data |= func->conf[i].enable;
  580. else
  581. data |= func->conf[i].disable;
  582. break;
  583. default:
  584. return -ENOTSUPP;
  585. }
  586. pcs->write(data, pcs->base + offset);
  587. return 0;
  588. }
  589. }
  590. return -ENOTSUPP;
  591. }
  592. static int pcs_pinconf_group_get(struct pinctrl_dev *pctldev,
  593. unsigned group, unsigned long *config)
  594. {
  595. const unsigned *pins;
  596. unsigned npins, old = 0;
  597. int i, ret;
  598. ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
  599. if (ret)
  600. return ret;
  601. for (i = 0; i < npins; i++) {
  602. if (pcs_pinconf_get(pctldev, pins[i], config))
  603. return -ENOTSUPP;
  604. /* configs do not match between two pins */
  605. if (i && (old != *config))
  606. return -ENOTSUPP;
  607. old = *config;
  608. }
  609. return 0;
  610. }
  611. static int pcs_pinconf_group_set(struct pinctrl_dev *pctldev,
  612. unsigned group, unsigned long config)
  613. {
  614. const unsigned *pins;
  615. unsigned npins;
  616. int i, ret;
  617. ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
  618. if (ret)
  619. return ret;
  620. for (i = 0; i < npins; i++) {
  621. if (pcs_pinconf_set(pctldev, pins[i], config))
  622. return -ENOTSUPP;
  623. }
  624. return 0;
  625. }
  626. static void pcs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  627. struct seq_file *s, unsigned pin)
  628. {
  629. }
  630. static void pcs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  631. struct seq_file *s, unsigned selector)
  632. {
  633. }
  634. static void pcs_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
  635. struct seq_file *s,
  636. unsigned long config)
  637. {
  638. pinconf_generic_dump_config(pctldev, s, config);
  639. }
  640. static const struct pinconf_ops pcs_pinconf_ops = {
  641. .pin_config_get = pcs_pinconf_get,
  642. .pin_config_set = pcs_pinconf_set,
  643. .pin_config_group_get = pcs_pinconf_group_get,
  644. .pin_config_group_set = pcs_pinconf_group_set,
  645. .pin_config_dbg_show = pcs_pinconf_dbg_show,
  646. .pin_config_group_dbg_show = pcs_pinconf_group_dbg_show,
  647. .pin_config_config_dbg_show = pcs_pinconf_config_dbg_show,
  648. .is_generic = true,
  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], shift;
  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. shift = ffs(value[1]) - 1;
  853. /* skip enable & disable */
  854. add_config(conf, param, value[0], 0, 0, value[1]);
  855. add_setting(settings, param, value[0] >> shift);
  856. }
  857. /* add pinconf setting with 4 parameters */
  858. static void pcs_add_conf4(struct pcs_device *pcs, struct device_node *np,
  859. const char *name, enum pin_config_param param,
  860. struct pcs_conf_vals **conf, unsigned long **settings)
  861. {
  862. unsigned value[4];
  863. int ret;
  864. /* value to set, enable, disable, mask */
  865. ret = of_property_read_u32_array(np, name, value, 4);
  866. if (ret)
  867. return;
  868. if (!value[3]) {
  869. dev_err(pcs->dev, "mask field of the property can't be 0\n");
  870. return;
  871. }
  872. value[0] &= value[3];
  873. value[1] &= value[3];
  874. value[2] &= value[3];
  875. ret = pcs_config_match(value[0], value[1], value[2]);
  876. if (ret < 0)
  877. dev_dbg(pcs->dev, "failed to match enable or disable bits\n");
  878. add_config(conf, param, value[0], value[1], value[2], value[3]);
  879. add_setting(settings, param, ret);
  880. }
  881. static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np,
  882. struct pcs_function *func,
  883. struct pinctrl_map **map)
  884. {
  885. struct pinctrl_map *m = *map;
  886. int i = 0, nconfs = 0;
  887. unsigned long *settings = NULL, *s = NULL;
  888. struct pcs_conf_vals *conf = NULL;
  889. struct pcs_conf_type prop2[] = {
  890. { "pinctrl-single,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, },
  891. { "pinctrl-single,slew-rate", PIN_CONFIG_SLEW_RATE, },
  892. { "pinctrl-single,input-schmitt", PIN_CONFIG_INPUT_SCHMITT, },
  893. };
  894. struct pcs_conf_type prop4[] = {
  895. { "pinctrl-single,bias-pullup", PIN_CONFIG_BIAS_PULL_UP, },
  896. { "pinctrl-single,bias-pulldown", PIN_CONFIG_BIAS_PULL_DOWN, },
  897. { "pinctrl-single,input-schmitt-enable",
  898. PIN_CONFIG_INPUT_SCHMITT_ENABLE, },
  899. };
  900. /* If pinconf isn't supported, don't parse properties in below. */
  901. if (!pcs->is_pinconf)
  902. return 0;
  903. /* cacluate how much properties are supported in current node */
  904. for (i = 0; i < ARRAY_SIZE(prop2); i++) {
  905. if (of_find_property(np, prop2[i].name, NULL))
  906. nconfs++;
  907. }
  908. for (i = 0; i < ARRAY_SIZE(prop4); i++) {
  909. if (of_find_property(np, prop4[i].name, NULL))
  910. nconfs++;
  911. }
  912. if (!nconfs)
  913. return 0;
  914. func->conf = devm_kzalloc(pcs->dev,
  915. sizeof(struct pcs_conf_vals) * nconfs,
  916. GFP_KERNEL);
  917. if (!func->conf)
  918. return -ENOMEM;
  919. func->nconfs = nconfs;
  920. conf = &(func->conf[0]);
  921. m++;
  922. settings = devm_kzalloc(pcs->dev, sizeof(unsigned long) * nconfs,
  923. GFP_KERNEL);
  924. if (!settings)
  925. return -ENOMEM;
  926. s = &settings[0];
  927. for (i = 0; i < ARRAY_SIZE(prop2); i++)
  928. pcs_add_conf2(pcs, np, prop2[i].name, prop2[i].param,
  929. &conf, &s);
  930. for (i = 0; i < ARRAY_SIZE(prop4); i++)
  931. pcs_add_conf4(pcs, np, prop4[i].name, prop4[i].param,
  932. &conf, &s);
  933. m->type = PIN_MAP_TYPE_CONFIGS_GROUP;
  934. m->data.configs.group_or_pin = np->name;
  935. m->data.configs.configs = settings;
  936. m->data.configs.num_configs = nconfs;
  937. return 0;
  938. }
  939. static void pcs_free_pingroups(struct pcs_device *pcs);
  940. /**
  941. * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
  942. * @pcs: pinctrl driver instance
  943. * @np: device node of the mux entry
  944. * @map: map entry
  945. * @num_maps: number of map
  946. * @pgnames: pingroup names
  947. *
  948. * Note that this binding currently supports only sets of one register + value.
  949. *
  950. * Also note that this driver tries to avoid understanding pin and function
  951. * names because of the extra bloat they would cause especially in the case of
  952. * a large number of pins. This driver just sets what is specified for the board
  953. * in the .dts file. Further user space debugging tools can be developed to
  954. * decipher the pin and function names using debugfs.
  955. *
  956. * If you are concerned about the boot time, set up the static pins in
  957. * the bootloader, and only set up selected pins as device tree entries.
  958. */
  959. static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
  960. struct device_node *np,
  961. struct pinctrl_map **map,
  962. unsigned *num_maps,
  963. const char **pgnames)
  964. {
  965. struct pcs_func_vals *vals;
  966. const __be32 *mux;
  967. int size, params, rows, *pins, index = 0, found = 0, res = -ENOMEM;
  968. struct pcs_function *function;
  969. if (pcs->bits_per_mux) {
  970. params = 3;
  971. mux = of_get_property(np, PCS_MUX_BITS_NAME, &size);
  972. } else {
  973. params = 2;
  974. mux = of_get_property(np, PCS_MUX_PINS_NAME, &size);
  975. }
  976. if (!mux) {
  977. dev_err(pcs->dev, "no valid property for %s\n", np->name);
  978. return -EINVAL;
  979. }
  980. if (size < (sizeof(*mux) * params)) {
  981. dev_err(pcs->dev, "bad data for %s\n", np->name);
  982. return -EINVAL;
  983. }
  984. size /= sizeof(*mux); /* Number of elements in array */
  985. rows = size / params;
  986. vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL);
  987. if (!vals)
  988. return -ENOMEM;
  989. pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL);
  990. if (!pins)
  991. goto free_vals;
  992. while (index < size) {
  993. unsigned offset, val;
  994. int pin;
  995. offset = be32_to_cpup(mux + index++);
  996. val = be32_to_cpup(mux + index++);
  997. vals[found].reg = pcs->base + offset;
  998. vals[found].val = val;
  999. if (params == 3) {
  1000. val = be32_to_cpup(mux + index++);
  1001. vals[found].mask = val;
  1002. }
  1003. pin = pcs_get_pin_by_offset(pcs, offset);
  1004. if (pin < 0) {
  1005. dev_err(pcs->dev,
  1006. "could not add functions for %s %ux\n",
  1007. np->name, offset);
  1008. break;
  1009. }
  1010. pins[found++] = pin;
  1011. }
  1012. pgnames[0] = np->name;
  1013. function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
  1014. if (!function)
  1015. goto free_pins;
  1016. res = pcs_add_pingroup(pcs, np, np->name, pins, found);
  1017. if (res < 0)
  1018. goto free_function;
  1019. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  1020. (*map)->data.mux.group = np->name;
  1021. (*map)->data.mux.function = np->name;
  1022. if (pcs->is_pinconf) {
  1023. if (pcs_parse_pinconf(pcs, np, function, map))
  1024. goto free_pingroups;
  1025. *num_maps = 2;
  1026. } else {
  1027. *num_maps = 1;
  1028. }
  1029. return 0;
  1030. free_pingroups:
  1031. pcs_free_pingroups(pcs);
  1032. *num_maps = 1;
  1033. free_function:
  1034. pcs_remove_function(pcs, function);
  1035. free_pins:
  1036. devm_kfree(pcs->dev, pins);
  1037. free_vals:
  1038. devm_kfree(pcs->dev, vals);
  1039. return res;
  1040. }
  1041. /**
  1042. * pcs_dt_node_to_map() - allocates and parses pinctrl maps
  1043. * @pctldev: pinctrl instance
  1044. * @np_config: device tree pinmux entry
  1045. * @map: array of map entries
  1046. * @num_maps: number of maps
  1047. */
  1048. static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
  1049. struct device_node *np_config,
  1050. struct pinctrl_map **map, unsigned *num_maps)
  1051. {
  1052. struct pcs_device *pcs;
  1053. const char **pgnames;
  1054. int ret;
  1055. pcs = pinctrl_dev_get_drvdata(pctldev);
  1056. /* create 2 maps. One is for pinmux, and the other is for pinconf. */
  1057. *map = devm_kzalloc(pcs->dev, sizeof(**map) * 2, GFP_KERNEL);
  1058. if (!*map)
  1059. return -ENOMEM;
  1060. *num_maps = 0;
  1061. pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL);
  1062. if (!pgnames) {
  1063. ret = -ENOMEM;
  1064. goto free_map;
  1065. }
  1066. ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map, num_maps,
  1067. pgnames);
  1068. if (ret < 0) {
  1069. dev_err(pcs->dev, "no pins entries for %s\n",
  1070. np_config->name);
  1071. goto free_pgnames;
  1072. }
  1073. return 0;
  1074. free_pgnames:
  1075. devm_kfree(pcs->dev, pgnames);
  1076. free_map:
  1077. devm_kfree(pcs->dev, *map);
  1078. return ret;
  1079. }
  1080. /**
  1081. * pcs_free_funcs() - free memory used by functions
  1082. * @pcs: pcs driver instance
  1083. */
  1084. static void pcs_free_funcs(struct pcs_device *pcs)
  1085. {
  1086. struct list_head *pos, *tmp;
  1087. int i;
  1088. mutex_lock(&pcs->mutex);
  1089. for (i = 0; i < pcs->nfuncs; i++) {
  1090. struct pcs_function *func;
  1091. func = radix_tree_lookup(&pcs->ftree, i);
  1092. if (!func)
  1093. continue;
  1094. radix_tree_delete(&pcs->ftree, i);
  1095. }
  1096. list_for_each_safe(pos, tmp, &pcs->functions) {
  1097. struct pcs_function *function;
  1098. function = list_entry(pos, struct pcs_function, node);
  1099. list_del(&function->node);
  1100. }
  1101. mutex_unlock(&pcs->mutex);
  1102. }
  1103. /**
  1104. * pcs_free_pingroups() - free memory used by pingroups
  1105. * @pcs: pcs driver instance
  1106. */
  1107. static void pcs_free_pingroups(struct pcs_device *pcs)
  1108. {
  1109. struct list_head *pos, *tmp;
  1110. int i;
  1111. mutex_lock(&pcs->mutex);
  1112. for (i = 0; i < pcs->ngroups; i++) {
  1113. struct pcs_pingroup *pingroup;
  1114. pingroup = radix_tree_lookup(&pcs->pgtree, i);
  1115. if (!pingroup)
  1116. continue;
  1117. radix_tree_delete(&pcs->pgtree, i);
  1118. }
  1119. list_for_each_safe(pos, tmp, &pcs->pingroups) {
  1120. struct pcs_pingroup *pingroup;
  1121. pingroup = list_entry(pos, struct pcs_pingroup, node);
  1122. list_del(&pingroup->node);
  1123. }
  1124. mutex_unlock(&pcs->mutex);
  1125. }
  1126. /**
  1127. * pcs_free_resources() - free memory used by this driver
  1128. * @pcs: pcs driver instance
  1129. */
  1130. static void pcs_free_resources(struct pcs_device *pcs)
  1131. {
  1132. if (pcs->pctl)
  1133. pinctrl_unregister(pcs->pctl);
  1134. pcs_free_funcs(pcs);
  1135. pcs_free_pingroups(pcs);
  1136. }
  1137. #define PCS_GET_PROP_U32(name, reg, err) \
  1138. do { \
  1139. ret = of_property_read_u32(np, name, reg); \
  1140. if (ret) { \
  1141. dev_err(pcs->dev, err); \
  1142. return ret; \
  1143. } \
  1144. } while (0);
  1145. static struct of_device_id pcs_of_match[];
  1146. static int pcs_add_gpio_func(struct device_node *node, struct pcs_device *pcs)
  1147. {
  1148. const char *propname = "pinctrl-single,gpio-range";
  1149. const char *cellname = "#pinctrl-single,gpio-range-cells";
  1150. struct of_phandle_args gpiospec;
  1151. struct pcs_gpiofunc_range *range;
  1152. int ret, i;
  1153. for (i = 0; ; i++) {
  1154. ret = of_parse_phandle_with_args(node, propname, cellname,
  1155. i, &gpiospec);
  1156. /* Do not treat it as error. Only treat it as end condition. */
  1157. if (ret) {
  1158. ret = 0;
  1159. break;
  1160. }
  1161. range = devm_kzalloc(pcs->dev, sizeof(*range), GFP_KERNEL);
  1162. if (!range) {
  1163. ret = -ENOMEM;
  1164. break;
  1165. }
  1166. range->offset = gpiospec.args[0];
  1167. range->npins = gpiospec.args[1];
  1168. range->gpiofunc = gpiospec.args[2];
  1169. mutex_lock(&pcs->mutex);
  1170. list_add_tail(&range->node, &pcs->gpiofuncs);
  1171. mutex_unlock(&pcs->mutex);
  1172. }
  1173. return ret;
  1174. }
  1175. static int pcs_probe(struct platform_device *pdev)
  1176. {
  1177. struct device_node *np = pdev->dev.of_node;
  1178. const struct of_device_id *match;
  1179. struct resource *res;
  1180. struct pcs_device *pcs;
  1181. int ret;
  1182. match = of_match_device(pcs_of_match, &pdev->dev);
  1183. if (!match)
  1184. return -EINVAL;
  1185. pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL);
  1186. if (!pcs) {
  1187. dev_err(&pdev->dev, "could not allocate\n");
  1188. return -ENOMEM;
  1189. }
  1190. pcs->dev = &pdev->dev;
  1191. mutex_init(&pcs->mutex);
  1192. INIT_LIST_HEAD(&pcs->pingroups);
  1193. INIT_LIST_HEAD(&pcs->functions);
  1194. INIT_LIST_HEAD(&pcs->gpiofuncs);
  1195. pcs->is_pinconf = match->data;
  1196. PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs->width,
  1197. "register width not specified\n");
  1198. ret = of_property_read_u32(np, "pinctrl-single,function-mask",
  1199. &pcs->fmask);
  1200. if (!ret) {
  1201. pcs->fshift = ffs(pcs->fmask) - 1;
  1202. pcs->fmax = pcs->fmask >> pcs->fshift;
  1203. } else {
  1204. /* If mask property doesn't exist, function mux is invalid. */
  1205. pcs->fmask = 0;
  1206. pcs->fshift = 0;
  1207. pcs->fmax = 0;
  1208. }
  1209. ret = of_property_read_u32(np, "pinctrl-single,function-off",
  1210. &pcs->foff);
  1211. if (ret)
  1212. pcs->foff = PCS_OFF_DISABLED;
  1213. pcs->bits_per_mux = of_property_read_bool(np,
  1214. "pinctrl-single,bit-per-mux");
  1215. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1216. if (!res) {
  1217. dev_err(pcs->dev, "could not get resource\n");
  1218. return -ENODEV;
  1219. }
  1220. pcs->res = devm_request_mem_region(pcs->dev, res->start,
  1221. resource_size(res), DRIVER_NAME);
  1222. if (!pcs->res) {
  1223. dev_err(pcs->dev, "could not get mem_region\n");
  1224. return -EBUSY;
  1225. }
  1226. pcs->size = resource_size(pcs->res);
  1227. pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size);
  1228. if (!pcs->base) {
  1229. dev_err(pcs->dev, "could not ioremap\n");
  1230. return -ENODEV;
  1231. }
  1232. INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL);
  1233. INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL);
  1234. platform_set_drvdata(pdev, pcs);
  1235. switch (pcs->width) {
  1236. case 8:
  1237. pcs->read = pcs_readb;
  1238. pcs->write = pcs_writeb;
  1239. break;
  1240. case 16:
  1241. pcs->read = pcs_readw;
  1242. pcs->write = pcs_writew;
  1243. break;
  1244. case 32:
  1245. pcs->read = pcs_readl;
  1246. pcs->write = pcs_writel;
  1247. break;
  1248. default:
  1249. break;
  1250. }
  1251. pcs->desc.name = DRIVER_NAME;
  1252. pcs->desc.pctlops = &pcs_pinctrl_ops;
  1253. pcs->desc.pmxops = &pcs_pinmux_ops;
  1254. if (pcs->is_pinconf)
  1255. pcs->desc.confops = &pcs_pinconf_ops;
  1256. pcs->desc.owner = THIS_MODULE;
  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");