pinctrl-single.c 50 KB

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