pinctrl-single.c 49 KB

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