pinctrl-single.c 48 KB

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