pinctrl-single.c 49 KB

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