pinctrl-single.c 40 KB

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