pinctrl-single.c 49 KB

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