pinctrl-single.c 50 KB

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