pinctrl-single.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  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/interrupt.h>
  18. #include <linux/irqchip/chained_irq.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/pinctrl/pinctrl.h>
  24. #include <linux/pinctrl/pinmux.h>
  25. #include <linux/pinctrl/pinconf-generic.h>
  26. #include <linux/platform_data/pinctrl-single.h>
  27. #include "core.h"
  28. #include "devicetree.h"
  29. #include "pinconf.h"
  30. #define DRIVER_NAME "pinctrl-single"
  31. #define PCS_OFF_DISABLED ~0U
  32. /**
  33. * struct pcs_pingroup - pingroups for a function
  34. * @np: pingroup device node pointer
  35. * @name: pingroup name
  36. * @gpins: array of the pins in the group
  37. * @ngpins: number of pins in the group
  38. * @node: list node
  39. */
  40. struct pcs_pingroup {
  41. struct device_node *np;
  42. const char *name;
  43. int *gpins;
  44. int ngpins;
  45. struct list_head node;
  46. };
  47. /**
  48. * struct pcs_func_vals - mux function register offset and value pair
  49. * @reg: register virtual address
  50. * @val: register value
  51. */
  52. struct pcs_func_vals {
  53. void __iomem *reg;
  54. unsigned val;
  55. unsigned mask;
  56. };
  57. /**
  58. * struct pcs_conf_vals - pinconf parameter, pinconf register offset
  59. * and value, enable, disable, mask
  60. * @param: config parameter
  61. * @val: user input bits in the pinconf register
  62. * @enable: enable bits in the pinconf register
  63. * @disable: disable bits in the pinconf register
  64. * @mask: mask bits in the register value
  65. */
  66. struct pcs_conf_vals {
  67. enum pin_config_param param;
  68. unsigned val;
  69. unsigned enable;
  70. unsigned disable;
  71. unsigned mask;
  72. };
  73. /**
  74. * struct pcs_conf_type - pinconf property name, pinconf param pair
  75. * @name: property name in DTS file
  76. * @param: config parameter
  77. */
  78. struct pcs_conf_type {
  79. const char *name;
  80. enum pin_config_param param;
  81. };
  82. /**
  83. * struct pcs_function - pinctrl function
  84. * @name: pinctrl function name
  85. * @vals: register and vals array
  86. * @nvals: number of entries in vals array
  87. * @pgnames: array of pingroup names the function uses
  88. * @npgnames: number of pingroup names the function uses
  89. * @node: list node
  90. */
  91. struct pcs_function {
  92. const char *name;
  93. struct pcs_func_vals *vals;
  94. unsigned nvals;
  95. const char **pgnames;
  96. int npgnames;
  97. struct pcs_conf_vals *conf;
  98. int nconfs;
  99. struct list_head node;
  100. };
  101. /**
  102. * struct pcs_gpiofunc_range - pin ranges with same mux value of gpio function
  103. * @offset: offset base of pins
  104. * @npins: number pins with the same mux value of gpio function
  105. * @gpiofunc: mux value of gpio function
  106. * @node: list node
  107. */
  108. struct pcs_gpiofunc_range {
  109. unsigned offset;
  110. unsigned npins;
  111. unsigned gpiofunc;
  112. struct list_head node;
  113. };
  114. /**
  115. * struct pcs_data - wrapper for data needed by pinctrl framework
  116. * @pa: pindesc array
  117. * @cur: index to current element
  118. *
  119. * REVISIT: We should be able to drop this eventually by adding
  120. * support for registering pins individually in the pinctrl
  121. * framework for those drivers that don't need a static array.
  122. */
  123. struct pcs_data {
  124. struct pinctrl_pin_desc *pa;
  125. int cur;
  126. };
  127. /**
  128. * struct pcs_soc_data - SoC specific settings
  129. * @flags: initial SoC specific PCS_FEAT_xxx values
  130. * @irq: optional interrupt for the controller
  131. * @irq_enable_mask: optional SoC specific interrupt enable mask
  132. * @irq_status_mask: optional SoC specific interrupt status mask
  133. * @rearm: optional SoC specific wake-up rearm function
  134. */
  135. struct pcs_soc_data {
  136. unsigned flags;
  137. int irq;
  138. unsigned irq_enable_mask;
  139. unsigned irq_status_mask;
  140. void (*rearm)(void);
  141. };
  142. /**
  143. * struct pcs_device - pinctrl device instance
  144. * @res: resources
  145. * @base: virtual address of the controller
  146. * @size: size of the ioremapped area
  147. * @dev: device entry
  148. * @np: device tree node
  149. * @pctl: pin controller device
  150. * @flags: mask of PCS_FEAT_xxx values
  151. * @missing_nr_pinctrl_cells: for legacy binding, may go away
  152. * @socdata: soc specific data
  153. * @lock: spinlock for register access
  154. * @mutex: mutex protecting the lists
  155. * @width: bits per mux register
  156. * @fmask: function register mask
  157. * @fshift: function register shift
  158. * @foff: value to turn mux off
  159. * @fmax: max number of functions in fmask
  160. * @bits_per_mux: number of bits per mux
  161. * @bits_per_pin: number of bits per pin
  162. * @pins: physical pins on the SoC
  163. * @pgtree: pingroup index radix tree
  164. * @ftree: function index radix tree
  165. * @pingroups: list of pingroups
  166. * @functions: list of functions
  167. * @gpiofuncs: list of gpio functions
  168. * @irqs: list of interrupt registers
  169. * @chip: chip container for this instance
  170. * @domain: IRQ domain for this instance
  171. * @ngroups: number of pingroups
  172. * @nfuncs: number of functions
  173. * @desc: pin controller descriptor
  174. * @read: register read function to use
  175. * @write: register write function to use
  176. */
  177. struct pcs_device {
  178. struct resource *res;
  179. void __iomem *base;
  180. unsigned size;
  181. struct device *dev;
  182. struct device_node *np;
  183. struct pinctrl_dev *pctl;
  184. unsigned flags;
  185. #define PCS_QUIRK_SHARED_IRQ (1 << 2)
  186. #define PCS_FEAT_IRQ (1 << 1)
  187. #define PCS_FEAT_PINCONF (1 << 0)
  188. struct property *missing_nr_pinctrl_cells;
  189. struct pcs_soc_data socdata;
  190. raw_spinlock_t lock;
  191. struct mutex mutex;
  192. unsigned width;
  193. unsigned fmask;
  194. unsigned fshift;
  195. unsigned foff;
  196. unsigned fmax;
  197. bool bits_per_mux;
  198. unsigned bits_per_pin;
  199. struct pcs_data pins;
  200. struct radix_tree_root pgtree;
  201. struct radix_tree_root ftree;
  202. struct list_head pingroups;
  203. struct list_head functions;
  204. struct list_head gpiofuncs;
  205. struct list_head irqs;
  206. struct irq_chip chip;
  207. struct irq_domain *domain;
  208. unsigned ngroups;
  209. unsigned nfuncs;
  210. struct pinctrl_desc desc;
  211. unsigned (*read)(void __iomem *reg);
  212. void (*write)(unsigned val, void __iomem *reg);
  213. };
  214. #define PCS_QUIRK_HAS_SHARED_IRQ (pcs->flags & PCS_QUIRK_SHARED_IRQ)
  215. #define PCS_HAS_IRQ (pcs->flags & PCS_FEAT_IRQ)
  216. #define PCS_HAS_PINCONF (pcs->flags & PCS_FEAT_PINCONF)
  217. static int pcs_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
  218. unsigned long *config);
  219. static int pcs_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
  220. unsigned long *configs, unsigned num_configs);
  221. static enum pin_config_param pcs_bias[] = {
  222. PIN_CONFIG_BIAS_PULL_DOWN,
  223. PIN_CONFIG_BIAS_PULL_UP,
  224. };
  225. /*
  226. * This lock class tells lockdep that irqchip core that this single
  227. * pinctrl can be in a different category than its parents, so it won't
  228. * report false recursion.
  229. */
  230. static struct lock_class_key pcs_lock_class;
  231. /*
  232. * REVISIT: Reads and writes could eventually use regmap or something
  233. * generic. But at least on omaps, some mux registers are performance
  234. * critical as they may need to be remuxed every time before and after
  235. * idle. Adding tests for register access width for every read and
  236. * write like regmap is doing is not desired, and caching the registers
  237. * does not help in this case.
  238. */
  239. static unsigned __maybe_unused pcs_readb(void __iomem *reg)
  240. {
  241. return readb(reg);
  242. }
  243. static unsigned __maybe_unused pcs_readw(void __iomem *reg)
  244. {
  245. return readw(reg);
  246. }
  247. static unsigned __maybe_unused pcs_readl(void __iomem *reg)
  248. {
  249. return readl(reg);
  250. }
  251. static void __maybe_unused pcs_writeb(unsigned val, void __iomem *reg)
  252. {
  253. writeb(val, reg);
  254. }
  255. static void __maybe_unused pcs_writew(unsigned val, void __iomem *reg)
  256. {
  257. writew(val, reg);
  258. }
  259. static void __maybe_unused pcs_writel(unsigned val, void __iomem *reg)
  260. {
  261. writel(val, reg);
  262. }
  263. static int pcs_get_groups_count(struct pinctrl_dev *pctldev)
  264. {
  265. struct pcs_device *pcs;
  266. pcs = pinctrl_dev_get_drvdata(pctldev);
  267. return pcs->ngroups;
  268. }
  269. static const char *pcs_get_group_name(struct pinctrl_dev *pctldev,
  270. unsigned gselector)
  271. {
  272. struct pcs_device *pcs;
  273. struct pcs_pingroup *group;
  274. pcs = pinctrl_dev_get_drvdata(pctldev);
  275. group = radix_tree_lookup(&pcs->pgtree, gselector);
  276. if (!group) {
  277. dev_err(pcs->dev, "%s could not find pingroup%i\n",
  278. __func__, gselector);
  279. return NULL;
  280. }
  281. return group->name;
  282. }
  283. static int pcs_get_group_pins(struct pinctrl_dev *pctldev,
  284. unsigned gselector,
  285. const unsigned **pins,
  286. unsigned *npins)
  287. {
  288. struct pcs_device *pcs;
  289. struct pcs_pingroup *group;
  290. pcs = pinctrl_dev_get_drvdata(pctldev);
  291. group = radix_tree_lookup(&pcs->pgtree, gselector);
  292. if (!group) {
  293. dev_err(pcs->dev, "%s could not find pingroup%i\n",
  294. __func__, gselector);
  295. return -EINVAL;
  296. }
  297. *pins = group->gpins;
  298. *npins = group->ngpins;
  299. return 0;
  300. }
  301. static void pcs_pin_dbg_show(struct pinctrl_dev *pctldev,
  302. struct seq_file *s,
  303. unsigned pin)
  304. {
  305. struct pcs_device *pcs;
  306. unsigned val, mux_bytes;
  307. unsigned long offset;
  308. size_t pa;
  309. pcs = pinctrl_dev_get_drvdata(pctldev);
  310. mux_bytes = pcs->width / BITS_PER_BYTE;
  311. offset = pin * mux_bytes;
  312. val = pcs->read(pcs->base + offset);
  313. pa = pcs->res->start + offset;
  314. seq_printf(s, "%zx %08x %s ", pa, val, DRIVER_NAME);
  315. }
  316. static void pcs_dt_free_map(struct pinctrl_dev *pctldev,
  317. struct pinctrl_map *map, unsigned num_maps)
  318. {
  319. struct pcs_device *pcs;
  320. pcs = pinctrl_dev_get_drvdata(pctldev);
  321. devm_kfree(pcs->dev, map);
  322. }
  323. static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
  324. struct device_node *np_config,
  325. struct pinctrl_map **map, unsigned *num_maps);
  326. static const struct pinctrl_ops pcs_pinctrl_ops = {
  327. .get_groups_count = pcs_get_groups_count,
  328. .get_group_name = pcs_get_group_name,
  329. .get_group_pins = pcs_get_group_pins,
  330. .pin_dbg_show = pcs_pin_dbg_show,
  331. .dt_node_to_map = pcs_dt_node_to_map,
  332. .dt_free_map = pcs_dt_free_map,
  333. };
  334. static int pcs_get_functions_count(struct pinctrl_dev *pctldev)
  335. {
  336. struct pcs_device *pcs;
  337. pcs = pinctrl_dev_get_drvdata(pctldev);
  338. return pcs->nfuncs;
  339. }
  340. static const char *pcs_get_function_name(struct pinctrl_dev *pctldev,
  341. unsigned fselector)
  342. {
  343. struct pcs_device *pcs;
  344. struct pcs_function *func;
  345. pcs = pinctrl_dev_get_drvdata(pctldev);
  346. func = radix_tree_lookup(&pcs->ftree, fselector);
  347. if (!func) {
  348. dev_err(pcs->dev, "%s could not find function%i\n",
  349. __func__, fselector);
  350. return NULL;
  351. }
  352. return func->name;
  353. }
  354. static int pcs_get_function_groups(struct pinctrl_dev *pctldev,
  355. unsigned fselector,
  356. const char * const **groups,
  357. unsigned * const ngroups)
  358. {
  359. struct pcs_device *pcs;
  360. struct pcs_function *func;
  361. pcs = pinctrl_dev_get_drvdata(pctldev);
  362. func = radix_tree_lookup(&pcs->ftree, fselector);
  363. if (!func) {
  364. dev_err(pcs->dev, "%s could not find function%i\n",
  365. __func__, fselector);
  366. return -EINVAL;
  367. }
  368. *groups = func->pgnames;
  369. *ngroups = func->npgnames;
  370. return 0;
  371. }
  372. static int pcs_get_function(struct pinctrl_dev *pctldev, unsigned pin,
  373. struct pcs_function **func)
  374. {
  375. struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
  376. struct pin_desc *pdesc = pin_desc_get(pctldev, pin);
  377. const struct pinctrl_setting_mux *setting;
  378. unsigned fselector;
  379. /* If pin is not described in DTS & enabled, mux_setting is NULL. */
  380. setting = pdesc->mux_setting;
  381. if (!setting)
  382. return -ENOTSUPP;
  383. fselector = setting->func;
  384. *func = radix_tree_lookup(&pcs->ftree, fselector);
  385. if (!(*func)) {
  386. dev_err(pcs->dev, "%s could not find function%i\n",
  387. __func__, fselector);
  388. return -ENOTSUPP;
  389. }
  390. return 0;
  391. }
  392. static int pcs_set_mux(struct pinctrl_dev *pctldev, unsigned fselector,
  393. unsigned group)
  394. {
  395. struct pcs_device *pcs;
  396. struct pcs_function *func;
  397. int i;
  398. pcs = pinctrl_dev_get_drvdata(pctldev);
  399. /* If function mask is null, needn't enable it. */
  400. if (!pcs->fmask)
  401. return 0;
  402. func = radix_tree_lookup(&pcs->ftree, fselector);
  403. if (!func)
  404. return -EINVAL;
  405. dev_dbg(pcs->dev, "enabling %s function%i\n",
  406. func->name, fselector);
  407. for (i = 0; i < func->nvals; i++) {
  408. struct pcs_func_vals *vals;
  409. unsigned long flags;
  410. unsigned val, mask;
  411. vals = &func->vals[i];
  412. raw_spin_lock_irqsave(&pcs->lock, flags);
  413. val = pcs->read(vals->reg);
  414. if (pcs->bits_per_mux)
  415. mask = vals->mask;
  416. else
  417. mask = pcs->fmask;
  418. val &= ~mask;
  419. val |= (vals->val & mask);
  420. pcs->write(val, vals->reg);
  421. raw_spin_unlock_irqrestore(&pcs->lock, flags);
  422. }
  423. return 0;
  424. }
  425. static int pcs_request_gpio(struct pinctrl_dev *pctldev,
  426. struct pinctrl_gpio_range *range, unsigned pin)
  427. {
  428. struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
  429. struct pcs_gpiofunc_range *frange = NULL;
  430. struct list_head *pos, *tmp;
  431. int mux_bytes = 0;
  432. unsigned data;
  433. /* If function mask is null, return directly. */
  434. if (!pcs->fmask)
  435. return -ENOTSUPP;
  436. list_for_each_safe(pos, tmp, &pcs->gpiofuncs) {
  437. frange = list_entry(pos, struct pcs_gpiofunc_range, node);
  438. if (pin >= frange->offset + frange->npins
  439. || pin < frange->offset)
  440. continue;
  441. mux_bytes = pcs->width / BITS_PER_BYTE;
  442. data = pcs->read(pcs->base + pin * mux_bytes) & ~pcs->fmask;
  443. data |= frange->gpiofunc;
  444. pcs->write(data, pcs->base + pin * mux_bytes);
  445. break;
  446. }
  447. return 0;
  448. }
  449. static const struct pinmux_ops pcs_pinmux_ops = {
  450. .get_functions_count = pcs_get_functions_count,
  451. .get_function_name = pcs_get_function_name,
  452. .get_function_groups = pcs_get_function_groups,
  453. .set_mux = pcs_set_mux,
  454. .gpio_request_enable = pcs_request_gpio,
  455. };
  456. /* Clear BIAS value */
  457. static void pcs_pinconf_clear_bias(struct pinctrl_dev *pctldev, unsigned pin)
  458. {
  459. unsigned long config;
  460. int i;
  461. for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) {
  462. config = pinconf_to_config_packed(pcs_bias[i], 0);
  463. pcs_pinconf_set(pctldev, pin, &config, 1);
  464. }
  465. }
  466. /*
  467. * Check whether PIN_CONFIG_BIAS_DISABLE is valid.
  468. * It's depend on that PULL_DOWN & PULL_UP configs are all invalid.
  469. */
  470. static bool pcs_pinconf_bias_disable(struct pinctrl_dev *pctldev, unsigned pin)
  471. {
  472. unsigned long config;
  473. int i;
  474. for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) {
  475. config = pinconf_to_config_packed(pcs_bias[i], 0);
  476. if (!pcs_pinconf_get(pctldev, pin, &config))
  477. goto out;
  478. }
  479. return true;
  480. out:
  481. return false;
  482. }
  483. static int pcs_pinconf_get(struct pinctrl_dev *pctldev,
  484. unsigned pin, unsigned long *config)
  485. {
  486. struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
  487. struct pcs_function *func;
  488. enum pin_config_param param;
  489. unsigned offset = 0, data = 0, i, j, ret;
  490. ret = pcs_get_function(pctldev, pin, &func);
  491. if (ret)
  492. return ret;
  493. for (i = 0; i < func->nconfs; i++) {
  494. param = pinconf_to_config_param(*config);
  495. if (param == PIN_CONFIG_BIAS_DISABLE) {
  496. if (pcs_pinconf_bias_disable(pctldev, pin)) {
  497. *config = 0;
  498. return 0;
  499. } else {
  500. return -ENOTSUPP;
  501. }
  502. } else if (param != func->conf[i].param) {
  503. continue;
  504. }
  505. offset = pin * (pcs->width / BITS_PER_BYTE);
  506. data = pcs->read(pcs->base + offset) & func->conf[i].mask;
  507. switch (func->conf[i].param) {
  508. /* 4 parameters */
  509. case PIN_CONFIG_BIAS_PULL_DOWN:
  510. case PIN_CONFIG_BIAS_PULL_UP:
  511. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  512. if ((data != func->conf[i].enable) ||
  513. (data == func->conf[i].disable))
  514. return -ENOTSUPP;
  515. *config = 0;
  516. break;
  517. /* 2 parameters */
  518. case PIN_CONFIG_INPUT_SCHMITT:
  519. for (j = 0; j < func->nconfs; j++) {
  520. switch (func->conf[j].param) {
  521. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  522. if (data != func->conf[j].enable)
  523. return -ENOTSUPP;
  524. break;
  525. default:
  526. break;
  527. }
  528. }
  529. *config = data;
  530. break;
  531. case PIN_CONFIG_DRIVE_STRENGTH:
  532. case PIN_CONFIG_SLEW_RATE:
  533. case PIN_CONFIG_LOW_POWER_MODE:
  534. default:
  535. *config = data;
  536. break;
  537. }
  538. return 0;
  539. }
  540. return -ENOTSUPP;
  541. }
  542. static int pcs_pinconf_set(struct pinctrl_dev *pctldev,
  543. unsigned pin, unsigned long *configs,
  544. unsigned num_configs)
  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. int j;
  551. ret = pcs_get_function(pctldev, pin, &func);
  552. if (ret)
  553. return ret;
  554. for (j = 0; j < num_configs; j++) {
  555. for (i = 0; i < func->nconfs; i++) {
  556. if (pinconf_to_config_param(configs[j])
  557. != func->conf[i].param)
  558. continue;
  559. offset = pin * (pcs->width / BITS_PER_BYTE);
  560. data = pcs->read(pcs->base + offset);
  561. arg = pinconf_to_config_argument(configs[j]);
  562. switch (func->conf[i].param) {
  563. /* 2 parameters */
  564. case PIN_CONFIG_INPUT_SCHMITT:
  565. case PIN_CONFIG_DRIVE_STRENGTH:
  566. case PIN_CONFIG_SLEW_RATE:
  567. case PIN_CONFIG_LOW_POWER_MODE:
  568. shift = ffs(func->conf[i].mask) - 1;
  569. data &= ~func->conf[i].mask;
  570. data |= (arg << shift) & func->conf[i].mask;
  571. break;
  572. /* 4 parameters */
  573. case PIN_CONFIG_BIAS_DISABLE:
  574. pcs_pinconf_clear_bias(pctldev, pin);
  575. break;
  576. case PIN_CONFIG_BIAS_PULL_DOWN:
  577. case PIN_CONFIG_BIAS_PULL_UP:
  578. if (arg)
  579. pcs_pinconf_clear_bias(pctldev, pin);
  580. /* fall through */
  581. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  582. data &= ~func->conf[i].mask;
  583. if (arg)
  584. data |= func->conf[i].enable;
  585. else
  586. data |= func->conf[i].disable;
  587. break;
  588. default:
  589. return -ENOTSUPP;
  590. }
  591. pcs->write(data, pcs->base + offset);
  592. break;
  593. }
  594. if (i >= func->nconfs)
  595. return -ENOTSUPP;
  596. } /* for each config */
  597. return 0;
  598. }
  599. static int pcs_pinconf_group_get(struct pinctrl_dev *pctldev,
  600. unsigned group, unsigned long *config)
  601. {
  602. const unsigned *pins;
  603. unsigned npins, old = 0;
  604. int i, ret;
  605. ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
  606. if (ret)
  607. return ret;
  608. for (i = 0; i < npins; i++) {
  609. if (pcs_pinconf_get(pctldev, pins[i], config))
  610. return -ENOTSUPP;
  611. /* configs do not match between two pins */
  612. if (i && (old != *config))
  613. return -ENOTSUPP;
  614. old = *config;
  615. }
  616. return 0;
  617. }
  618. static int pcs_pinconf_group_set(struct pinctrl_dev *pctldev,
  619. unsigned group, unsigned long *configs,
  620. unsigned num_configs)
  621. {
  622. const unsigned *pins;
  623. unsigned npins;
  624. int i, ret;
  625. ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
  626. if (ret)
  627. return ret;
  628. for (i = 0; i < npins; i++) {
  629. if (pcs_pinconf_set(pctldev, pins[i], configs, num_configs))
  630. return -ENOTSUPP;
  631. }
  632. return 0;
  633. }
  634. static void pcs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  635. struct seq_file *s, unsigned pin)
  636. {
  637. }
  638. static void pcs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  639. struct seq_file *s, unsigned selector)
  640. {
  641. }
  642. static void pcs_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
  643. struct seq_file *s,
  644. unsigned long config)
  645. {
  646. pinconf_generic_dump_config(pctldev, s, config);
  647. }
  648. static const struct pinconf_ops pcs_pinconf_ops = {
  649. .pin_config_get = pcs_pinconf_get,
  650. .pin_config_set = pcs_pinconf_set,
  651. .pin_config_group_get = pcs_pinconf_group_get,
  652. .pin_config_group_set = pcs_pinconf_group_set,
  653. .pin_config_dbg_show = pcs_pinconf_dbg_show,
  654. .pin_config_group_dbg_show = pcs_pinconf_group_dbg_show,
  655. .pin_config_config_dbg_show = pcs_pinconf_config_dbg_show,
  656. .is_generic = true,
  657. };
  658. /**
  659. * pcs_add_pin() - add a pin to the static per controller pin array
  660. * @pcs: pcs driver instance
  661. * @offset: register offset from base
  662. */
  663. static int pcs_add_pin(struct pcs_device *pcs, unsigned offset,
  664. unsigned pin_pos)
  665. {
  666. struct pcs_soc_data *pcs_soc = &pcs->socdata;
  667. struct pinctrl_pin_desc *pin;
  668. int i;
  669. i = pcs->pins.cur;
  670. if (i >= pcs->desc.npins) {
  671. dev_err(pcs->dev, "too many pins, max %i\n",
  672. pcs->desc.npins);
  673. return -ENOMEM;
  674. }
  675. if (pcs_soc->irq_enable_mask) {
  676. unsigned val;
  677. val = pcs->read(pcs->base + offset);
  678. if (val & pcs_soc->irq_enable_mask) {
  679. dev_dbg(pcs->dev, "irq enabled at boot for pin at %lx (%x), clearing\n",
  680. (unsigned long)pcs->res->start + offset, val);
  681. val &= ~pcs_soc->irq_enable_mask;
  682. pcs->write(val, pcs->base + offset);
  683. }
  684. }
  685. pin = &pcs->pins.pa[i];
  686. pin->number = i;
  687. pcs->pins.cur++;
  688. return i;
  689. }
  690. /**
  691. * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver
  692. * @pcs: pcs driver instance
  693. *
  694. * In case of errors, resources are freed in pcs_free_resources.
  695. *
  696. * If your hardware needs holes in the address space, then just set
  697. * up multiple driver instances.
  698. */
  699. static int pcs_allocate_pin_table(struct pcs_device *pcs)
  700. {
  701. int mux_bytes, nr_pins, i;
  702. int num_pins_in_register = 0;
  703. mux_bytes = pcs->width / BITS_PER_BYTE;
  704. if (pcs->bits_per_mux) {
  705. pcs->bits_per_pin = fls(pcs->fmask);
  706. nr_pins = (pcs->size * BITS_PER_BYTE) / pcs->bits_per_pin;
  707. num_pins_in_register = pcs->width / pcs->bits_per_pin;
  708. } else {
  709. nr_pins = pcs->size / mux_bytes;
  710. }
  711. dev_dbg(pcs->dev, "allocating %i pins\n", nr_pins);
  712. pcs->pins.pa = devm_kzalloc(pcs->dev,
  713. sizeof(*pcs->pins.pa) * nr_pins,
  714. GFP_KERNEL);
  715. if (!pcs->pins.pa)
  716. return -ENOMEM;
  717. pcs->desc.pins = pcs->pins.pa;
  718. pcs->desc.npins = nr_pins;
  719. for (i = 0; i < pcs->desc.npins; i++) {
  720. unsigned offset;
  721. int res;
  722. int byte_num;
  723. int pin_pos = 0;
  724. if (pcs->bits_per_mux) {
  725. byte_num = (pcs->bits_per_pin * i) / BITS_PER_BYTE;
  726. offset = (byte_num / mux_bytes) * mux_bytes;
  727. pin_pos = i % num_pins_in_register;
  728. } else {
  729. offset = i * mux_bytes;
  730. }
  731. res = pcs_add_pin(pcs, offset, pin_pos);
  732. if (res < 0) {
  733. dev_err(pcs->dev, "error adding pins: %i\n", res);
  734. return res;
  735. }
  736. }
  737. return 0;
  738. }
  739. /**
  740. * pcs_add_function() - adds a new function to the function list
  741. * @pcs: pcs driver instance
  742. * @np: device node of the mux entry
  743. * @name: name of the function
  744. * @vals: array of mux register value pairs used by the function
  745. * @nvals: number of mux register value pairs
  746. * @pgnames: array of pingroup names for the function
  747. * @npgnames: number of pingroup names
  748. */
  749. static struct pcs_function *pcs_add_function(struct pcs_device *pcs,
  750. struct device_node *np,
  751. const char *name,
  752. struct pcs_func_vals *vals,
  753. unsigned nvals,
  754. const char **pgnames,
  755. unsigned npgnames)
  756. {
  757. struct pcs_function *function;
  758. function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL);
  759. if (!function)
  760. return NULL;
  761. function->name = name;
  762. function->vals = vals;
  763. function->nvals = nvals;
  764. function->pgnames = pgnames;
  765. function->npgnames = npgnames;
  766. mutex_lock(&pcs->mutex);
  767. list_add_tail(&function->node, &pcs->functions);
  768. radix_tree_insert(&pcs->ftree, pcs->nfuncs, function);
  769. pcs->nfuncs++;
  770. mutex_unlock(&pcs->mutex);
  771. return function;
  772. }
  773. static void pcs_remove_function(struct pcs_device *pcs,
  774. struct pcs_function *function)
  775. {
  776. int i;
  777. mutex_lock(&pcs->mutex);
  778. for (i = 0; i < pcs->nfuncs; i++) {
  779. struct pcs_function *found;
  780. found = radix_tree_lookup(&pcs->ftree, i);
  781. if (found == function)
  782. radix_tree_delete(&pcs->ftree, i);
  783. }
  784. list_del(&function->node);
  785. mutex_unlock(&pcs->mutex);
  786. }
  787. /**
  788. * pcs_add_pingroup() - add a pingroup to the pingroup list
  789. * @pcs: pcs driver instance
  790. * @np: device node of the mux entry
  791. * @name: name of the pingroup
  792. * @gpins: array of the pins that belong to the group
  793. * @ngpins: number of pins in the group
  794. */
  795. static int pcs_add_pingroup(struct pcs_device *pcs,
  796. struct device_node *np,
  797. const char *name,
  798. int *gpins,
  799. int ngpins)
  800. {
  801. struct pcs_pingroup *pingroup;
  802. pingroup = devm_kzalloc(pcs->dev, sizeof(*pingroup), GFP_KERNEL);
  803. if (!pingroup)
  804. return -ENOMEM;
  805. pingroup->name = name;
  806. pingroup->np = np;
  807. pingroup->gpins = gpins;
  808. pingroup->ngpins = ngpins;
  809. mutex_lock(&pcs->mutex);
  810. list_add_tail(&pingroup->node, &pcs->pingroups);
  811. radix_tree_insert(&pcs->pgtree, pcs->ngroups, pingroup);
  812. pcs->ngroups++;
  813. mutex_unlock(&pcs->mutex);
  814. return 0;
  815. }
  816. /**
  817. * pcs_get_pin_by_offset() - get a pin index based on the register offset
  818. * @pcs: pcs driver instance
  819. * @offset: register offset from the base
  820. *
  821. * Note that this is OK as long as the pins are in a static array.
  822. */
  823. static int pcs_get_pin_by_offset(struct pcs_device *pcs, unsigned offset)
  824. {
  825. unsigned index;
  826. if (offset >= pcs->size) {
  827. dev_err(pcs->dev, "mux offset out of range: 0x%x (0x%x)\n",
  828. offset, pcs->size);
  829. return -EINVAL;
  830. }
  831. if (pcs->bits_per_mux)
  832. index = (offset * BITS_PER_BYTE) / pcs->bits_per_pin;
  833. else
  834. index = offset / (pcs->width / BITS_PER_BYTE);
  835. return index;
  836. }
  837. /*
  838. * check whether data matches enable bits or disable bits
  839. * Return value: 1 for matching enable bits, 0 for matching disable bits,
  840. * and negative value for matching failure.
  841. */
  842. static int pcs_config_match(unsigned data, unsigned enable, unsigned disable)
  843. {
  844. int ret = -EINVAL;
  845. if (data == enable)
  846. ret = 1;
  847. else if (data == disable)
  848. ret = 0;
  849. return ret;
  850. }
  851. static void add_config(struct pcs_conf_vals **conf, enum pin_config_param param,
  852. unsigned value, unsigned enable, unsigned disable,
  853. unsigned mask)
  854. {
  855. (*conf)->param = param;
  856. (*conf)->val = value;
  857. (*conf)->enable = enable;
  858. (*conf)->disable = disable;
  859. (*conf)->mask = mask;
  860. (*conf)++;
  861. }
  862. static void add_setting(unsigned long **setting, enum pin_config_param param,
  863. unsigned arg)
  864. {
  865. **setting = pinconf_to_config_packed(param, arg);
  866. (*setting)++;
  867. }
  868. /* add pinconf setting with 2 parameters */
  869. static void pcs_add_conf2(struct pcs_device *pcs, struct device_node *np,
  870. const char *name, enum pin_config_param param,
  871. struct pcs_conf_vals **conf, unsigned long **settings)
  872. {
  873. unsigned value[2], shift;
  874. int ret;
  875. ret = of_property_read_u32_array(np, name, value, 2);
  876. if (ret)
  877. return;
  878. /* set value & mask */
  879. value[0] &= value[1];
  880. shift = ffs(value[1]) - 1;
  881. /* skip enable & disable */
  882. add_config(conf, param, value[0], 0, 0, value[1]);
  883. add_setting(settings, param, value[0] >> shift);
  884. }
  885. /* add pinconf setting with 4 parameters */
  886. static void pcs_add_conf4(struct pcs_device *pcs, struct device_node *np,
  887. const char *name, enum pin_config_param param,
  888. struct pcs_conf_vals **conf, unsigned long **settings)
  889. {
  890. unsigned value[4];
  891. int ret;
  892. /* value to set, enable, disable, mask */
  893. ret = of_property_read_u32_array(np, name, value, 4);
  894. if (ret)
  895. return;
  896. if (!value[3]) {
  897. dev_err(pcs->dev, "mask field of the property can't be 0\n");
  898. return;
  899. }
  900. value[0] &= value[3];
  901. value[1] &= value[3];
  902. value[2] &= value[3];
  903. ret = pcs_config_match(value[0], value[1], value[2]);
  904. if (ret < 0)
  905. dev_dbg(pcs->dev, "failed to match enable or disable bits\n");
  906. add_config(conf, param, value[0], value[1], value[2], value[3]);
  907. add_setting(settings, param, ret);
  908. }
  909. static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np,
  910. struct pcs_function *func,
  911. struct pinctrl_map **map)
  912. {
  913. struct pinctrl_map *m = *map;
  914. int i = 0, nconfs = 0;
  915. unsigned long *settings = NULL, *s = NULL;
  916. struct pcs_conf_vals *conf = NULL;
  917. struct pcs_conf_type prop2[] = {
  918. { "pinctrl-single,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, },
  919. { "pinctrl-single,slew-rate", PIN_CONFIG_SLEW_RATE, },
  920. { "pinctrl-single,input-schmitt", PIN_CONFIG_INPUT_SCHMITT, },
  921. { "pinctrl-single,low-power-mode", PIN_CONFIG_LOW_POWER_MODE, },
  922. };
  923. struct pcs_conf_type prop4[] = {
  924. { "pinctrl-single,bias-pullup", PIN_CONFIG_BIAS_PULL_UP, },
  925. { "pinctrl-single,bias-pulldown", PIN_CONFIG_BIAS_PULL_DOWN, },
  926. { "pinctrl-single,input-schmitt-enable",
  927. PIN_CONFIG_INPUT_SCHMITT_ENABLE, },
  928. };
  929. /* If pinconf isn't supported, don't parse properties in below. */
  930. if (!PCS_HAS_PINCONF)
  931. return 0;
  932. /* cacluate how much properties are supported in current node */
  933. for (i = 0; i < ARRAY_SIZE(prop2); i++) {
  934. if (of_find_property(np, prop2[i].name, NULL))
  935. nconfs++;
  936. }
  937. for (i = 0; i < ARRAY_SIZE(prop4); i++) {
  938. if (of_find_property(np, prop4[i].name, NULL))
  939. nconfs++;
  940. }
  941. if (!nconfs)
  942. return 0;
  943. func->conf = devm_kzalloc(pcs->dev,
  944. sizeof(struct pcs_conf_vals) * nconfs,
  945. GFP_KERNEL);
  946. if (!func->conf)
  947. return -ENOMEM;
  948. func->nconfs = nconfs;
  949. conf = &(func->conf[0]);
  950. m++;
  951. settings = devm_kzalloc(pcs->dev, sizeof(unsigned long) * nconfs,
  952. GFP_KERNEL);
  953. if (!settings)
  954. return -ENOMEM;
  955. s = &settings[0];
  956. for (i = 0; i < ARRAY_SIZE(prop2); i++)
  957. pcs_add_conf2(pcs, np, prop2[i].name, prop2[i].param,
  958. &conf, &s);
  959. for (i = 0; i < ARRAY_SIZE(prop4); i++)
  960. pcs_add_conf4(pcs, np, prop4[i].name, prop4[i].param,
  961. &conf, &s);
  962. m->type = PIN_MAP_TYPE_CONFIGS_GROUP;
  963. m->data.configs.group_or_pin = np->name;
  964. m->data.configs.configs = settings;
  965. m->data.configs.num_configs = nconfs;
  966. return 0;
  967. }
  968. static void pcs_free_pingroups(struct pcs_device *pcs);
  969. /**
  970. * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
  971. * @pcs: pinctrl driver instance
  972. * @np: device node of the mux entry
  973. * @map: map entry
  974. * @num_maps: number of map
  975. * @pgnames: pingroup names
  976. *
  977. * Note that this binding currently supports only sets of one register + value.
  978. *
  979. * Also note that this driver tries to avoid understanding pin and function
  980. * names because of the extra bloat they would cause especially in the case of
  981. * a large number of pins. This driver just sets what is specified for the board
  982. * in the .dts file. Further user space debugging tools can be developed to
  983. * decipher the pin and function names using debugfs.
  984. *
  985. * If you are concerned about the boot time, set up the static pins in
  986. * the bootloader, and only set up selected pins as device tree entries.
  987. */
  988. static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
  989. struct device_node *np,
  990. struct pinctrl_map **map,
  991. unsigned *num_maps,
  992. const char **pgnames)
  993. {
  994. const char *name = "pinctrl-single,pins";
  995. struct pcs_func_vals *vals;
  996. int rows, *pins, found = 0, res = -ENOMEM, i;
  997. struct pcs_function *function;
  998. rows = pinctrl_count_index_with_args(np, name);
  999. if (rows <= 0) {
  1000. dev_err(pcs->dev, "Ivalid number of rows: %d\n", rows);
  1001. return -EINVAL;
  1002. }
  1003. vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL);
  1004. if (!vals)
  1005. return -ENOMEM;
  1006. pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL);
  1007. if (!pins)
  1008. goto free_vals;
  1009. for (i = 0; i < rows; i++) {
  1010. struct of_phandle_args pinctrl_spec;
  1011. unsigned int offset;
  1012. int pin;
  1013. res = pinctrl_parse_index_with_args(np, name, i, &pinctrl_spec);
  1014. if (res)
  1015. return res;
  1016. if (pinctrl_spec.args_count < 2) {
  1017. dev_err(pcs->dev, "invalid args_count for spec: %i\n",
  1018. pinctrl_spec.args_count);
  1019. break;
  1020. }
  1021. /* Index plus one value cell */
  1022. offset = pinctrl_spec.args[0];
  1023. vals[found].reg = pcs->base + offset;
  1024. vals[found].val = pinctrl_spec.args[1];
  1025. dev_dbg(pcs->dev, "%s index: 0x%x value: 0x%x\n",
  1026. pinctrl_spec.np->name, offset, pinctrl_spec.args[1]);
  1027. pin = pcs_get_pin_by_offset(pcs, offset);
  1028. if (pin < 0) {
  1029. dev_err(pcs->dev,
  1030. "could not add functions for %s %ux\n",
  1031. np->name, offset);
  1032. break;
  1033. }
  1034. pins[found++] = pin;
  1035. }
  1036. pgnames[0] = np->name;
  1037. function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
  1038. if (!function) {
  1039. res = -ENOMEM;
  1040. goto free_pins;
  1041. }
  1042. res = pcs_add_pingroup(pcs, np, np->name, pins, found);
  1043. if (res < 0)
  1044. goto free_function;
  1045. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  1046. (*map)->data.mux.group = np->name;
  1047. (*map)->data.mux.function = np->name;
  1048. if (PCS_HAS_PINCONF) {
  1049. res = pcs_parse_pinconf(pcs, np, function, map);
  1050. if (res)
  1051. goto free_pingroups;
  1052. *num_maps = 2;
  1053. } else {
  1054. *num_maps = 1;
  1055. }
  1056. return 0;
  1057. free_pingroups:
  1058. pcs_free_pingroups(pcs);
  1059. *num_maps = 1;
  1060. free_function:
  1061. pcs_remove_function(pcs, function);
  1062. free_pins:
  1063. devm_kfree(pcs->dev, pins);
  1064. free_vals:
  1065. devm_kfree(pcs->dev, vals);
  1066. return res;
  1067. }
  1068. static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
  1069. struct device_node *np,
  1070. struct pinctrl_map **map,
  1071. unsigned *num_maps,
  1072. const char **pgnames)
  1073. {
  1074. const char *name = "pinctrl-single,bits";
  1075. struct pcs_func_vals *vals;
  1076. int rows, *pins, found = 0, res = -ENOMEM, i;
  1077. int npins_in_row;
  1078. struct pcs_function *function;
  1079. rows = pinctrl_count_index_with_args(np, name);
  1080. if (rows <= 0) {
  1081. dev_err(pcs->dev, "Invalid number of rows: %d\n", rows);
  1082. return -EINVAL;
  1083. }
  1084. npins_in_row = pcs->width / pcs->bits_per_pin;
  1085. vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows * npins_in_row,
  1086. GFP_KERNEL);
  1087. if (!vals)
  1088. return -ENOMEM;
  1089. pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows * npins_in_row,
  1090. GFP_KERNEL);
  1091. if (!pins)
  1092. goto free_vals;
  1093. for (i = 0; i < rows; i++) {
  1094. struct of_phandle_args pinctrl_spec;
  1095. unsigned offset, val;
  1096. unsigned mask, bit_pos, val_pos, mask_pos, submask;
  1097. unsigned pin_num_from_lsb;
  1098. int pin;
  1099. res = pinctrl_parse_index_with_args(np, name, i, &pinctrl_spec);
  1100. if (res)
  1101. return res;
  1102. if (pinctrl_spec.args_count < 3) {
  1103. dev_err(pcs->dev, "invalid args_count for spec: %i\n",
  1104. pinctrl_spec.args_count);
  1105. break;
  1106. }
  1107. /* Index plus two value cells */
  1108. offset = pinctrl_spec.args[0];
  1109. val = pinctrl_spec.args[1];
  1110. mask = pinctrl_spec.args[2];
  1111. dev_dbg(pcs->dev, "%s index: 0x%x value: 0x%x mask: 0x%x\n",
  1112. pinctrl_spec.np->name, offset, val, mask);
  1113. /* Parse pins in each row from LSB */
  1114. while (mask) {
  1115. bit_pos = __ffs(mask);
  1116. pin_num_from_lsb = bit_pos / pcs->bits_per_pin;
  1117. mask_pos = ((pcs->fmask) << bit_pos);
  1118. val_pos = val & mask_pos;
  1119. submask = mask & mask_pos;
  1120. if ((mask & mask_pos) == 0) {
  1121. dev_err(pcs->dev,
  1122. "Invalid mask for %s at 0x%x\n",
  1123. np->name, offset);
  1124. break;
  1125. }
  1126. mask &= ~mask_pos;
  1127. if (submask != mask_pos) {
  1128. dev_warn(pcs->dev,
  1129. "Invalid submask 0x%x for %s at 0x%x\n",
  1130. submask, np->name, offset);
  1131. continue;
  1132. }
  1133. vals[found].mask = submask;
  1134. vals[found].reg = pcs->base + offset;
  1135. vals[found].val = val_pos;
  1136. pin = pcs_get_pin_by_offset(pcs, offset);
  1137. if (pin < 0) {
  1138. dev_err(pcs->dev,
  1139. "could not add functions for %s %ux\n",
  1140. np->name, offset);
  1141. break;
  1142. }
  1143. pins[found++] = pin + pin_num_from_lsb;
  1144. }
  1145. }
  1146. pgnames[0] = np->name;
  1147. function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
  1148. if (!function) {
  1149. res = -ENOMEM;
  1150. goto free_pins;
  1151. }
  1152. res = pcs_add_pingroup(pcs, np, np->name, pins, found);
  1153. if (res < 0)
  1154. goto free_function;
  1155. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  1156. (*map)->data.mux.group = np->name;
  1157. (*map)->data.mux.function = np->name;
  1158. if (PCS_HAS_PINCONF) {
  1159. dev_err(pcs->dev, "pinconf not supported\n");
  1160. goto free_pingroups;
  1161. }
  1162. *num_maps = 1;
  1163. return 0;
  1164. free_pingroups:
  1165. pcs_free_pingroups(pcs);
  1166. *num_maps = 1;
  1167. free_function:
  1168. pcs_remove_function(pcs, function);
  1169. free_pins:
  1170. devm_kfree(pcs->dev, pins);
  1171. free_vals:
  1172. devm_kfree(pcs->dev, vals);
  1173. return res;
  1174. }
  1175. /**
  1176. * pcs_dt_node_to_map() - allocates and parses pinctrl maps
  1177. * @pctldev: pinctrl instance
  1178. * @np_config: device tree pinmux entry
  1179. * @map: array of map entries
  1180. * @num_maps: number of maps
  1181. */
  1182. static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
  1183. struct device_node *np_config,
  1184. struct pinctrl_map **map, unsigned *num_maps)
  1185. {
  1186. struct pcs_device *pcs;
  1187. const char **pgnames;
  1188. int ret;
  1189. pcs = pinctrl_dev_get_drvdata(pctldev);
  1190. /* create 2 maps. One is for pinmux, and the other is for pinconf. */
  1191. *map = devm_kzalloc(pcs->dev, sizeof(**map) * 2, GFP_KERNEL);
  1192. if (!*map)
  1193. return -ENOMEM;
  1194. *num_maps = 0;
  1195. pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL);
  1196. if (!pgnames) {
  1197. ret = -ENOMEM;
  1198. goto free_map;
  1199. }
  1200. if (pcs->bits_per_mux) {
  1201. ret = pcs_parse_bits_in_pinctrl_entry(pcs, np_config, map,
  1202. num_maps, pgnames);
  1203. if (ret < 0) {
  1204. dev_err(pcs->dev, "no pins entries for %s\n",
  1205. np_config->name);
  1206. goto free_pgnames;
  1207. }
  1208. } else {
  1209. ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map,
  1210. num_maps, pgnames);
  1211. if (ret < 0) {
  1212. dev_err(pcs->dev, "no pins entries for %s\n",
  1213. np_config->name);
  1214. goto free_pgnames;
  1215. }
  1216. }
  1217. return 0;
  1218. free_pgnames:
  1219. devm_kfree(pcs->dev, pgnames);
  1220. free_map:
  1221. devm_kfree(pcs->dev, *map);
  1222. return ret;
  1223. }
  1224. /**
  1225. * pcs_free_funcs() - free memory used by functions
  1226. * @pcs: pcs driver instance
  1227. */
  1228. static void pcs_free_funcs(struct pcs_device *pcs)
  1229. {
  1230. struct list_head *pos, *tmp;
  1231. int i;
  1232. mutex_lock(&pcs->mutex);
  1233. for (i = 0; i < pcs->nfuncs; i++) {
  1234. struct pcs_function *func;
  1235. func = radix_tree_lookup(&pcs->ftree, i);
  1236. if (!func)
  1237. continue;
  1238. radix_tree_delete(&pcs->ftree, i);
  1239. }
  1240. list_for_each_safe(pos, tmp, &pcs->functions) {
  1241. struct pcs_function *function;
  1242. function = list_entry(pos, struct pcs_function, node);
  1243. list_del(&function->node);
  1244. }
  1245. mutex_unlock(&pcs->mutex);
  1246. }
  1247. /**
  1248. * pcs_free_pingroups() - free memory used by pingroups
  1249. * @pcs: pcs driver instance
  1250. */
  1251. static void pcs_free_pingroups(struct pcs_device *pcs)
  1252. {
  1253. struct list_head *pos, *tmp;
  1254. int i;
  1255. mutex_lock(&pcs->mutex);
  1256. for (i = 0; i < pcs->ngroups; i++) {
  1257. struct pcs_pingroup *pingroup;
  1258. pingroup = radix_tree_lookup(&pcs->pgtree, i);
  1259. if (!pingroup)
  1260. continue;
  1261. radix_tree_delete(&pcs->pgtree, i);
  1262. }
  1263. list_for_each_safe(pos, tmp, &pcs->pingroups) {
  1264. struct pcs_pingroup *pingroup;
  1265. pingroup = list_entry(pos, struct pcs_pingroup, node);
  1266. list_del(&pingroup->node);
  1267. }
  1268. mutex_unlock(&pcs->mutex);
  1269. }
  1270. /**
  1271. * pcs_irq_free() - free interrupt
  1272. * @pcs: pcs driver instance
  1273. */
  1274. static void pcs_irq_free(struct pcs_device *pcs)
  1275. {
  1276. struct pcs_soc_data *pcs_soc = &pcs->socdata;
  1277. if (pcs_soc->irq < 0)
  1278. return;
  1279. if (pcs->domain)
  1280. irq_domain_remove(pcs->domain);
  1281. if (PCS_QUIRK_HAS_SHARED_IRQ)
  1282. free_irq(pcs_soc->irq, pcs_soc);
  1283. else
  1284. irq_set_chained_handler(pcs_soc->irq, NULL);
  1285. }
  1286. /**
  1287. * pcs_free_resources() - free memory used by this driver
  1288. * @pcs: pcs driver instance
  1289. */
  1290. static void pcs_free_resources(struct pcs_device *pcs)
  1291. {
  1292. pcs_irq_free(pcs);
  1293. pinctrl_unregister(pcs->pctl);
  1294. pcs_free_funcs(pcs);
  1295. pcs_free_pingroups(pcs);
  1296. #if IS_BUILTIN(CONFIG_PINCTRL_SINGLE)
  1297. if (pcs->missing_nr_pinctrl_cells)
  1298. of_remove_property(pcs->np, pcs->missing_nr_pinctrl_cells);
  1299. #endif
  1300. }
  1301. static const struct of_device_id pcs_of_match[];
  1302. static int pcs_add_gpio_func(struct device_node *node, struct pcs_device *pcs)
  1303. {
  1304. const char *propname = "pinctrl-single,gpio-range";
  1305. const char *cellname = "#pinctrl-single,gpio-range-cells";
  1306. struct of_phandle_args gpiospec;
  1307. struct pcs_gpiofunc_range *range;
  1308. int ret, i;
  1309. for (i = 0; ; i++) {
  1310. ret = of_parse_phandle_with_args(node, propname, cellname,
  1311. i, &gpiospec);
  1312. /* Do not treat it as error. Only treat it as end condition. */
  1313. if (ret) {
  1314. ret = 0;
  1315. break;
  1316. }
  1317. range = devm_kzalloc(pcs->dev, sizeof(*range), GFP_KERNEL);
  1318. if (!range) {
  1319. ret = -ENOMEM;
  1320. break;
  1321. }
  1322. range->offset = gpiospec.args[0];
  1323. range->npins = gpiospec.args[1];
  1324. range->gpiofunc = gpiospec.args[2];
  1325. mutex_lock(&pcs->mutex);
  1326. list_add_tail(&range->node, &pcs->gpiofuncs);
  1327. mutex_unlock(&pcs->mutex);
  1328. }
  1329. return ret;
  1330. }
  1331. /**
  1332. * @reg: virtual address of interrupt register
  1333. * @hwirq: hardware irq number
  1334. * @irq: virtual irq number
  1335. * @node: list node
  1336. */
  1337. struct pcs_interrupt {
  1338. void __iomem *reg;
  1339. irq_hw_number_t hwirq;
  1340. unsigned int irq;
  1341. struct list_head node;
  1342. };
  1343. /**
  1344. * pcs_irq_set() - enables or disables an interrupt
  1345. *
  1346. * Note that this currently assumes one interrupt per pinctrl
  1347. * register that is typically used for wake-up events.
  1348. */
  1349. static inline void pcs_irq_set(struct pcs_soc_data *pcs_soc,
  1350. int irq, const bool enable)
  1351. {
  1352. struct pcs_device *pcs;
  1353. struct list_head *pos;
  1354. unsigned mask;
  1355. pcs = container_of(pcs_soc, struct pcs_device, socdata);
  1356. list_for_each(pos, &pcs->irqs) {
  1357. struct pcs_interrupt *pcswi;
  1358. unsigned soc_mask;
  1359. pcswi = list_entry(pos, struct pcs_interrupt, node);
  1360. if (irq != pcswi->irq)
  1361. continue;
  1362. soc_mask = pcs_soc->irq_enable_mask;
  1363. raw_spin_lock(&pcs->lock);
  1364. mask = pcs->read(pcswi->reg);
  1365. if (enable)
  1366. mask |= soc_mask;
  1367. else
  1368. mask &= ~soc_mask;
  1369. pcs->write(mask, pcswi->reg);
  1370. /* flush posted write */
  1371. mask = pcs->read(pcswi->reg);
  1372. raw_spin_unlock(&pcs->lock);
  1373. }
  1374. if (pcs_soc->rearm)
  1375. pcs_soc->rearm();
  1376. }
  1377. /**
  1378. * pcs_irq_mask() - mask pinctrl interrupt
  1379. * @d: interrupt data
  1380. */
  1381. static void pcs_irq_mask(struct irq_data *d)
  1382. {
  1383. struct pcs_soc_data *pcs_soc = irq_data_get_irq_chip_data(d);
  1384. pcs_irq_set(pcs_soc, d->irq, false);
  1385. }
  1386. /**
  1387. * pcs_irq_unmask() - unmask pinctrl interrupt
  1388. * @d: interrupt data
  1389. */
  1390. static void pcs_irq_unmask(struct irq_data *d)
  1391. {
  1392. struct pcs_soc_data *pcs_soc = irq_data_get_irq_chip_data(d);
  1393. pcs_irq_set(pcs_soc, d->irq, true);
  1394. }
  1395. /**
  1396. * pcs_irq_set_wake() - toggle the suspend and resume wake up
  1397. * @d: interrupt data
  1398. * @state: wake-up state
  1399. *
  1400. * Note that this should be called only for suspend and resume.
  1401. * For runtime PM, the wake-up events should be enabled by default.
  1402. */
  1403. static int pcs_irq_set_wake(struct irq_data *d, unsigned int state)
  1404. {
  1405. if (state)
  1406. pcs_irq_unmask(d);
  1407. else
  1408. pcs_irq_mask(d);
  1409. return 0;
  1410. }
  1411. /**
  1412. * pcs_irq_handle() - common interrupt handler
  1413. * @pcs_irq: interrupt data
  1414. *
  1415. * Note that this currently assumes we have one interrupt bit per
  1416. * mux register. This interrupt is typically used for wake-up events.
  1417. * For more complex interrupts different handlers can be specified.
  1418. */
  1419. static int pcs_irq_handle(struct pcs_soc_data *pcs_soc)
  1420. {
  1421. struct pcs_device *pcs;
  1422. struct list_head *pos;
  1423. int count = 0;
  1424. pcs = container_of(pcs_soc, struct pcs_device, socdata);
  1425. list_for_each(pos, &pcs->irqs) {
  1426. struct pcs_interrupt *pcswi;
  1427. unsigned mask;
  1428. pcswi = list_entry(pos, struct pcs_interrupt, node);
  1429. raw_spin_lock(&pcs->lock);
  1430. mask = pcs->read(pcswi->reg);
  1431. raw_spin_unlock(&pcs->lock);
  1432. if (mask & pcs_soc->irq_status_mask) {
  1433. generic_handle_irq(irq_find_mapping(pcs->domain,
  1434. pcswi->hwirq));
  1435. count++;
  1436. }
  1437. }
  1438. return count;
  1439. }
  1440. /**
  1441. * pcs_irq_handler() - handler for the shared interrupt case
  1442. * @irq: interrupt
  1443. * @d: data
  1444. *
  1445. * Use this for cases where multiple instances of
  1446. * pinctrl-single share a single interrupt like on omaps.
  1447. */
  1448. static irqreturn_t pcs_irq_handler(int irq, void *d)
  1449. {
  1450. struct pcs_soc_data *pcs_soc = d;
  1451. return pcs_irq_handle(pcs_soc) ? IRQ_HANDLED : IRQ_NONE;
  1452. }
  1453. /**
  1454. * pcs_irq_handle() - handler for the dedicated chained interrupt case
  1455. * @irq: interrupt
  1456. * @desc: interrupt descriptor
  1457. *
  1458. * Use this if you have a separate interrupt for each
  1459. * pinctrl-single instance.
  1460. */
  1461. static void pcs_irq_chain_handler(struct irq_desc *desc)
  1462. {
  1463. struct pcs_soc_data *pcs_soc = irq_desc_get_handler_data(desc);
  1464. struct irq_chip *chip;
  1465. chip = irq_desc_get_chip(desc);
  1466. chained_irq_enter(chip, desc);
  1467. pcs_irq_handle(pcs_soc);
  1468. /* REVISIT: export and add handle_bad_irq(irq, desc)? */
  1469. chained_irq_exit(chip, desc);
  1470. return;
  1471. }
  1472. static int pcs_irqdomain_map(struct irq_domain *d, unsigned int irq,
  1473. irq_hw_number_t hwirq)
  1474. {
  1475. struct pcs_soc_data *pcs_soc = d->host_data;
  1476. struct pcs_device *pcs;
  1477. struct pcs_interrupt *pcswi;
  1478. pcs = container_of(pcs_soc, struct pcs_device, socdata);
  1479. pcswi = devm_kzalloc(pcs->dev, sizeof(*pcswi), GFP_KERNEL);
  1480. if (!pcswi)
  1481. return -ENOMEM;
  1482. pcswi->reg = pcs->base + hwirq;
  1483. pcswi->hwirq = hwirq;
  1484. pcswi->irq = irq;
  1485. mutex_lock(&pcs->mutex);
  1486. list_add_tail(&pcswi->node, &pcs->irqs);
  1487. mutex_unlock(&pcs->mutex);
  1488. irq_set_chip_data(irq, pcs_soc);
  1489. irq_set_chip_and_handler(irq, &pcs->chip,
  1490. handle_level_irq);
  1491. irq_set_lockdep_class(irq, &pcs_lock_class);
  1492. irq_set_noprobe(irq);
  1493. return 0;
  1494. }
  1495. static const struct irq_domain_ops pcs_irqdomain_ops = {
  1496. .map = pcs_irqdomain_map,
  1497. .xlate = irq_domain_xlate_onecell,
  1498. };
  1499. /**
  1500. * pcs_irq_init_chained_handler() - set up a chained interrupt handler
  1501. * @pcs: pcs driver instance
  1502. * @np: device node pointer
  1503. */
  1504. static int pcs_irq_init_chained_handler(struct pcs_device *pcs,
  1505. struct device_node *np)
  1506. {
  1507. struct pcs_soc_data *pcs_soc = &pcs->socdata;
  1508. const char *name = "pinctrl";
  1509. int num_irqs;
  1510. if (!pcs_soc->irq_enable_mask ||
  1511. !pcs_soc->irq_status_mask) {
  1512. pcs_soc->irq = -1;
  1513. return -EINVAL;
  1514. }
  1515. INIT_LIST_HEAD(&pcs->irqs);
  1516. pcs->chip.name = name;
  1517. pcs->chip.irq_ack = pcs_irq_mask;
  1518. pcs->chip.irq_mask = pcs_irq_mask;
  1519. pcs->chip.irq_unmask = pcs_irq_unmask;
  1520. pcs->chip.irq_set_wake = pcs_irq_set_wake;
  1521. if (PCS_QUIRK_HAS_SHARED_IRQ) {
  1522. int res;
  1523. res = request_irq(pcs_soc->irq, pcs_irq_handler,
  1524. IRQF_SHARED | IRQF_NO_SUSPEND |
  1525. IRQF_NO_THREAD,
  1526. name, pcs_soc);
  1527. if (res) {
  1528. pcs_soc->irq = -1;
  1529. return res;
  1530. }
  1531. } else {
  1532. irq_set_chained_handler_and_data(pcs_soc->irq,
  1533. pcs_irq_chain_handler,
  1534. pcs_soc);
  1535. }
  1536. /*
  1537. * We can use the register offset as the hardirq
  1538. * number as irq_domain_add_simple maps them lazily.
  1539. * This way we can easily support more than one
  1540. * interrupt per function if needed.
  1541. */
  1542. num_irqs = pcs->size;
  1543. pcs->domain = irq_domain_add_simple(np, num_irqs, 0,
  1544. &pcs_irqdomain_ops,
  1545. pcs_soc);
  1546. if (!pcs->domain) {
  1547. irq_set_chained_handler(pcs_soc->irq, NULL);
  1548. return -EINVAL;
  1549. }
  1550. return 0;
  1551. }
  1552. #ifdef CONFIG_PM
  1553. static int pinctrl_single_suspend(struct platform_device *pdev,
  1554. pm_message_t state)
  1555. {
  1556. struct pcs_device *pcs;
  1557. pcs = platform_get_drvdata(pdev);
  1558. if (!pcs)
  1559. return -EINVAL;
  1560. return pinctrl_force_sleep(pcs->pctl);
  1561. }
  1562. static int pinctrl_single_resume(struct platform_device *pdev)
  1563. {
  1564. struct pcs_device *pcs;
  1565. pcs = platform_get_drvdata(pdev);
  1566. if (!pcs)
  1567. return -EINVAL;
  1568. return pinctrl_force_default(pcs->pctl);
  1569. }
  1570. #endif
  1571. /**
  1572. * pcs_quirk_missing_pinctrl_cells - handle legacy binding
  1573. * @pcs: pinctrl driver instance
  1574. * @np: device tree node
  1575. * @cells: number of cells
  1576. *
  1577. * Handle legacy binding with no #pinctrl-cells. This should be
  1578. * always two pinctrl-single,bit-per-mux and one for others.
  1579. * At some point we may want to consider removing this.
  1580. */
  1581. static int pcs_quirk_missing_pinctrl_cells(struct pcs_device *pcs,
  1582. struct device_node *np,
  1583. int cells)
  1584. {
  1585. struct property *p;
  1586. const char *name = "#pinctrl-cells";
  1587. int error;
  1588. u32 val;
  1589. error = of_property_read_u32(np, name, &val);
  1590. if (!error)
  1591. return 0;
  1592. dev_warn(pcs->dev, "please update dts to use %s = <%i>\n",
  1593. name, cells);
  1594. p = devm_kzalloc(pcs->dev, sizeof(*p), GFP_KERNEL);
  1595. if (!p)
  1596. return -ENOMEM;
  1597. p->length = sizeof(__be32);
  1598. p->value = devm_kzalloc(pcs->dev, sizeof(__be32), GFP_KERNEL);
  1599. if (!p->value)
  1600. return -ENOMEM;
  1601. *(__be32 *)p->value = cpu_to_be32(cells);
  1602. p->name = devm_kstrdup(pcs->dev, name, GFP_KERNEL);
  1603. if (!p->name)
  1604. return -ENOMEM;
  1605. pcs->missing_nr_pinctrl_cells = p;
  1606. #if IS_BUILTIN(CONFIG_PINCTRL_SINGLE)
  1607. error = of_add_property(np, pcs->missing_nr_pinctrl_cells);
  1608. #endif
  1609. return error;
  1610. }
  1611. static int pcs_probe(struct platform_device *pdev)
  1612. {
  1613. struct device_node *np = pdev->dev.of_node;
  1614. const struct of_device_id *match;
  1615. struct pcs_pdata *pdata;
  1616. struct resource *res;
  1617. struct pcs_device *pcs;
  1618. const struct pcs_soc_data *soc;
  1619. int ret;
  1620. match = of_match_device(pcs_of_match, &pdev->dev);
  1621. if (!match)
  1622. return -EINVAL;
  1623. pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL);
  1624. if (!pcs) {
  1625. dev_err(&pdev->dev, "could not allocate\n");
  1626. return -ENOMEM;
  1627. }
  1628. pcs->dev = &pdev->dev;
  1629. pcs->np = np;
  1630. raw_spin_lock_init(&pcs->lock);
  1631. mutex_init(&pcs->mutex);
  1632. INIT_LIST_HEAD(&pcs->pingroups);
  1633. INIT_LIST_HEAD(&pcs->functions);
  1634. INIT_LIST_HEAD(&pcs->gpiofuncs);
  1635. soc = match->data;
  1636. pcs->flags = soc->flags;
  1637. memcpy(&pcs->socdata, soc, sizeof(*soc));
  1638. ret = of_property_read_u32(np, "pinctrl-single,register-width",
  1639. &pcs->width);
  1640. if (ret) {
  1641. dev_err(pcs->dev, "register width not specified\n");
  1642. return ret;
  1643. }
  1644. ret = of_property_read_u32(np, "pinctrl-single,function-mask",
  1645. &pcs->fmask);
  1646. if (!ret) {
  1647. pcs->fshift = __ffs(pcs->fmask);
  1648. pcs->fmax = pcs->fmask >> pcs->fshift;
  1649. } else {
  1650. /* If mask property doesn't exist, function mux is invalid. */
  1651. pcs->fmask = 0;
  1652. pcs->fshift = 0;
  1653. pcs->fmax = 0;
  1654. }
  1655. ret = of_property_read_u32(np, "pinctrl-single,function-off",
  1656. &pcs->foff);
  1657. if (ret)
  1658. pcs->foff = PCS_OFF_DISABLED;
  1659. pcs->bits_per_mux = of_property_read_bool(np,
  1660. "pinctrl-single,bit-per-mux");
  1661. ret = pcs_quirk_missing_pinctrl_cells(pcs, np,
  1662. pcs->bits_per_mux ? 2 : 1);
  1663. if (ret) {
  1664. dev_err(&pdev->dev, "unable to patch #pinctrl-cells\n");
  1665. return ret;
  1666. }
  1667. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1668. if (!res) {
  1669. dev_err(pcs->dev, "could not get resource\n");
  1670. return -ENODEV;
  1671. }
  1672. pcs->res = devm_request_mem_region(pcs->dev, res->start,
  1673. resource_size(res), DRIVER_NAME);
  1674. if (!pcs->res) {
  1675. dev_err(pcs->dev, "could not get mem_region\n");
  1676. return -EBUSY;
  1677. }
  1678. pcs->size = resource_size(pcs->res);
  1679. pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size);
  1680. if (!pcs->base) {
  1681. dev_err(pcs->dev, "could not ioremap\n");
  1682. return -ENODEV;
  1683. }
  1684. INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL);
  1685. INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL);
  1686. platform_set_drvdata(pdev, pcs);
  1687. switch (pcs->width) {
  1688. case 8:
  1689. pcs->read = pcs_readb;
  1690. pcs->write = pcs_writeb;
  1691. break;
  1692. case 16:
  1693. pcs->read = pcs_readw;
  1694. pcs->write = pcs_writew;
  1695. break;
  1696. case 32:
  1697. pcs->read = pcs_readl;
  1698. pcs->write = pcs_writel;
  1699. break;
  1700. default:
  1701. break;
  1702. }
  1703. pcs->desc.name = DRIVER_NAME;
  1704. pcs->desc.pctlops = &pcs_pinctrl_ops;
  1705. pcs->desc.pmxops = &pcs_pinmux_ops;
  1706. if (PCS_HAS_PINCONF)
  1707. pcs->desc.confops = &pcs_pinconf_ops;
  1708. pcs->desc.owner = THIS_MODULE;
  1709. ret = pcs_allocate_pin_table(pcs);
  1710. if (ret < 0)
  1711. goto free;
  1712. pcs->pctl = pinctrl_register(&pcs->desc, pcs->dev, pcs);
  1713. if (IS_ERR(pcs->pctl)) {
  1714. dev_err(pcs->dev, "could not register single pinctrl driver\n");
  1715. ret = PTR_ERR(pcs->pctl);
  1716. goto free;
  1717. }
  1718. ret = pcs_add_gpio_func(np, pcs);
  1719. if (ret < 0)
  1720. goto free;
  1721. pcs->socdata.irq = irq_of_parse_and_map(np, 0);
  1722. if (pcs->socdata.irq)
  1723. pcs->flags |= PCS_FEAT_IRQ;
  1724. /* We still need auxdata for some omaps for PRM interrupts */
  1725. pdata = dev_get_platdata(&pdev->dev);
  1726. if (pdata) {
  1727. if (pdata->rearm)
  1728. pcs->socdata.rearm = pdata->rearm;
  1729. if (pdata->irq) {
  1730. pcs->socdata.irq = pdata->irq;
  1731. pcs->flags |= PCS_FEAT_IRQ;
  1732. }
  1733. }
  1734. if (PCS_HAS_IRQ) {
  1735. ret = pcs_irq_init_chained_handler(pcs, np);
  1736. if (ret < 0)
  1737. dev_warn(pcs->dev, "initialized with no interrupts\n");
  1738. }
  1739. dev_info(pcs->dev, "%i pins at pa %p size %u\n",
  1740. pcs->desc.npins, pcs->base, pcs->size);
  1741. return 0;
  1742. free:
  1743. pcs_free_resources(pcs);
  1744. return ret;
  1745. }
  1746. static int pcs_remove(struct platform_device *pdev)
  1747. {
  1748. struct pcs_device *pcs = platform_get_drvdata(pdev);
  1749. if (!pcs)
  1750. return 0;
  1751. pcs_free_resources(pcs);
  1752. return 0;
  1753. }
  1754. static const struct pcs_soc_data pinctrl_single_omap_wkup = {
  1755. .flags = PCS_QUIRK_SHARED_IRQ,
  1756. .irq_enable_mask = (1 << 14), /* OMAP_WAKEUP_EN */
  1757. .irq_status_mask = (1 << 15), /* OMAP_WAKEUP_EVENT */
  1758. };
  1759. static const struct pcs_soc_data pinctrl_single_dra7 = {
  1760. .irq_enable_mask = (1 << 24), /* WAKEUPENABLE */
  1761. .irq_status_mask = (1 << 25), /* WAKEUPEVENT */
  1762. };
  1763. static const struct pcs_soc_data pinctrl_single_am437x = {
  1764. .flags = PCS_QUIRK_SHARED_IRQ,
  1765. .irq_enable_mask = (1 << 29), /* OMAP_WAKEUP_EN */
  1766. .irq_status_mask = (1 << 30), /* OMAP_WAKEUP_EVENT */
  1767. };
  1768. static const struct pcs_soc_data pinctrl_single = {
  1769. };
  1770. static const struct pcs_soc_data pinconf_single = {
  1771. .flags = PCS_FEAT_PINCONF,
  1772. };
  1773. static const struct of_device_id pcs_of_match[] = {
  1774. { .compatible = "ti,omap3-padconf", .data = &pinctrl_single_omap_wkup },
  1775. { .compatible = "ti,omap4-padconf", .data = &pinctrl_single_omap_wkup },
  1776. { .compatible = "ti,omap5-padconf", .data = &pinctrl_single_omap_wkup },
  1777. { .compatible = "ti,dra7-padconf", .data = &pinctrl_single_dra7 },
  1778. { .compatible = "ti,am437-padconf", .data = &pinctrl_single_am437x },
  1779. { .compatible = "pinctrl-single", .data = &pinctrl_single },
  1780. { .compatible = "pinconf-single", .data = &pinconf_single },
  1781. { },
  1782. };
  1783. MODULE_DEVICE_TABLE(of, pcs_of_match);
  1784. static struct platform_driver pcs_driver = {
  1785. .probe = pcs_probe,
  1786. .remove = pcs_remove,
  1787. .driver = {
  1788. .name = DRIVER_NAME,
  1789. .of_match_table = pcs_of_match,
  1790. },
  1791. #ifdef CONFIG_PM
  1792. .suspend = pinctrl_single_suspend,
  1793. .resume = pinctrl_single_resume,
  1794. #endif
  1795. };
  1796. module_platform_driver(pcs_driver);
  1797. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  1798. MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver");
  1799. MODULE_LICENSE("GPL v2");