pinctrl-single.c 49 KB

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