pinctrl-single.c 49 KB

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