pinctrl-single.c 45 KB

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