pinctrl-single.c 48 KB

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