pinctrl-single.c 49 KB

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