pinctrl-single.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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) + 1)
  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. {
  659. struct pinctrl_pin_desc *pin;
  660. struct pcs_name *pn;
  661. int i;
  662. i = pcs->pins.cur;
  663. if (i >= pcs->desc.npins) {
  664. dev_err(pcs->dev, "too many pins, max %i\n",
  665. pcs->desc.npins);
  666. return -ENOMEM;
  667. }
  668. pin = &pcs->pins.pa[i];
  669. pn = &pcs->names[i];
  670. sprintf(pn->name, "%lx",
  671. (unsigned long)pcs->res->start + offset);
  672. pin->name = pn->name;
  673. pin->number = i;
  674. pcs->pins.cur++;
  675. return i;
  676. }
  677. /**
  678. * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver
  679. * @pcs: pcs driver instance
  680. *
  681. * In case of errors, resources are freed in pcs_free_resources.
  682. *
  683. * If your hardware needs holes in the address space, then just set
  684. * up multiple driver instances.
  685. */
  686. static int pcs_allocate_pin_table(struct pcs_device *pcs)
  687. {
  688. int mux_bytes, nr_pins, i;
  689. mux_bytes = pcs->width / BITS_PER_BYTE;
  690. if (pcs->bits_per_mux) {
  691. pcs->bits_per_pin = fls(pcs->fmask);
  692. nr_pins = (pcs->size * BITS_PER_BYTE) / pcs->bits_per_pin;
  693. } else {
  694. nr_pins = pcs->size / mux_bytes;
  695. }
  696. dev_dbg(pcs->dev, "allocating %i pins\n", nr_pins);
  697. pcs->pins.pa = devm_kzalloc(pcs->dev,
  698. sizeof(*pcs->pins.pa) * nr_pins,
  699. GFP_KERNEL);
  700. if (!pcs->pins.pa)
  701. return -ENOMEM;
  702. pcs->names = devm_kzalloc(pcs->dev,
  703. sizeof(struct pcs_name) * nr_pins,
  704. GFP_KERNEL);
  705. if (!pcs->names)
  706. return -ENOMEM;
  707. pcs->desc.pins = pcs->pins.pa;
  708. pcs->desc.npins = nr_pins;
  709. for (i = 0; i < pcs->desc.npins; i++) {
  710. unsigned offset;
  711. int res;
  712. int byte_num;
  713. if (pcs->bits_per_mux) {
  714. byte_num = (pcs->bits_per_pin * i) / BITS_PER_BYTE;
  715. offset = (byte_num / mux_bytes) * mux_bytes;
  716. } else {
  717. offset = i * mux_bytes;
  718. }
  719. res = pcs_add_pin(pcs, offset);
  720. if (res < 0) {
  721. dev_err(pcs->dev, "error adding pins: %i\n", res);
  722. return res;
  723. }
  724. }
  725. return 0;
  726. }
  727. /**
  728. * pcs_add_function() - adds a new function to the function list
  729. * @pcs: pcs driver instance
  730. * @np: device node of the mux entry
  731. * @name: name of the function
  732. * @vals: array of mux register value pairs used by the function
  733. * @nvals: number of mux register value pairs
  734. * @pgnames: array of pingroup names for the function
  735. * @npgnames: number of pingroup names
  736. */
  737. static struct pcs_function *pcs_add_function(struct pcs_device *pcs,
  738. struct device_node *np,
  739. const char *name,
  740. struct pcs_func_vals *vals,
  741. unsigned nvals,
  742. const char **pgnames,
  743. unsigned npgnames)
  744. {
  745. struct pcs_function *function;
  746. function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL);
  747. if (!function)
  748. return NULL;
  749. function->name = name;
  750. function->vals = vals;
  751. function->nvals = nvals;
  752. function->pgnames = pgnames;
  753. function->npgnames = npgnames;
  754. mutex_lock(&pcs->mutex);
  755. list_add_tail(&function->node, &pcs->functions);
  756. radix_tree_insert(&pcs->ftree, pcs->nfuncs, function);
  757. pcs->nfuncs++;
  758. mutex_unlock(&pcs->mutex);
  759. return function;
  760. }
  761. static void pcs_remove_function(struct pcs_device *pcs,
  762. struct pcs_function *function)
  763. {
  764. int i;
  765. mutex_lock(&pcs->mutex);
  766. for (i = 0; i < pcs->nfuncs; i++) {
  767. struct pcs_function *found;
  768. found = radix_tree_lookup(&pcs->ftree, i);
  769. if (found == function)
  770. radix_tree_delete(&pcs->ftree, i);
  771. }
  772. list_del(&function->node);
  773. mutex_unlock(&pcs->mutex);
  774. }
  775. /**
  776. * pcs_add_pingroup() - add a pingroup to the pingroup list
  777. * @pcs: pcs driver instance
  778. * @np: device node of the mux entry
  779. * @name: name of the pingroup
  780. * @gpins: array of the pins that belong to the group
  781. * @ngpins: number of pins in the group
  782. */
  783. static int pcs_add_pingroup(struct pcs_device *pcs,
  784. struct device_node *np,
  785. const char *name,
  786. int *gpins,
  787. int ngpins)
  788. {
  789. struct pcs_pingroup *pingroup;
  790. pingroup = devm_kzalloc(pcs->dev, sizeof(*pingroup), GFP_KERNEL);
  791. if (!pingroup)
  792. return -ENOMEM;
  793. pingroup->name = name;
  794. pingroup->np = np;
  795. pingroup->gpins = gpins;
  796. pingroup->ngpins = ngpins;
  797. mutex_lock(&pcs->mutex);
  798. list_add_tail(&pingroup->node, &pcs->pingroups);
  799. radix_tree_insert(&pcs->pgtree, pcs->ngroups, pingroup);
  800. pcs->ngroups++;
  801. mutex_unlock(&pcs->mutex);
  802. return 0;
  803. }
  804. /**
  805. * pcs_get_pin_by_offset() - get a pin index based on the register offset
  806. * @pcs: pcs driver instance
  807. * @offset: register offset from the base
  808. *
  809. * Note that this is OK as long as the pins are in a static array.
  810. */
  811. static int pcs_get_pin_by_offset(struct pcs_device *pcs, unsigned offset)
  812. {
  813. unsigned index;
  814. if (offset >= pcs->size) {
  815. dev_err(pcs->dev, "mux offset out of range: 0x%x (0x%x)\n",
  816. offset, pcs->size);
  817. return -EINVAL;
  818. }
  819. if (pcs->bits_per_mux)
  820. index = (offset * BITS_PER_BYTE) / pcs->bits_per_pin;
  821. else
  822. index = offset / (pcs->width / BITS_PER_BYTE);
  823. return index;
  824. }
  825. /*
  826. * check whether data matches enable bits or disable bits
  827. * Return value: 1 for matching enable bits, 0 for matching disable bits,
  828. * and negative value for matching failure.
  829. */
  830. static int pcs_config_match(unsigned data, unsigned enable, unsigned disable)
  831. {
  832. int ret = -EINVAL;
  833. if (data == enable)
  834. ret = 1;
  835. else if (data == disable)
  836. ret = 0;
  837. return ret;
  838. }
  839. static void add_config(struct pcs_conf_vals **conf, enum pin_config_param param,
  840. unsigned value, unsigned enable, unsigned disable,
  841. unsigned mask)
  842. {
  843. (*conf)->param = param;
  844. (*conf)->val = value;
  845. (*conf)->enable = enable;
  846. (*conf)->disable = disable;
  847. (*conf)->mask = mask;
  848. (*conf)++;
  849. }
  850. static void add_setting(unsigned long **setting, enum pin_config_param param,
  851. unsigned arg)
  852. {
  853. **setting = pinconf_to_config_packed(param, arg);
  854. (*setting)++;
  855. }
  856. /* add pinconf setting with 2 parameters */
  857. static void pcs_add_conf2(struct pcs_device *pcs, struct device_node *np,
  858. const char *name, enum pin_config_param param,
  859. struct pcs_conf_vals **conf, unsigned long **settings)
  860. {
  861. unsigned value[2], shift;
  862. int ret;
  863. ret = of_property_read_u32_array(np, name, value, 2);
  864. if (ret)
  865. return;
  866. /* set value & mask */
  867. value[0] &= value[1];
  868. shift = ffs(value[1]) - 1;
  869. /* skip enable & disable */
  870. add_config(conf, param, value[0], 0, 0, value[1]);
  871. add_setting(settings, param, value[0] >> shift);
  872. }
  873. /* add pinconf setting with 4 parameters */
  874. static void pcs_add_conf4(struct pcs_device *pcs, struct device_node *np,
  875. const char *name, enum pin_config_param param,
  876. struct pcs_conf_vals **conf, unsigned long **settings)
  877. {
  878. unsigned value[4];
  879. int ret;
  880. /* value to set, enable, disable, mask */
  881. ret = of_property_read_u32_array(np, name, value, 4);
  882. if (ret)
  883. return;
  884. if (!value[3]) {
  885. dev_err(pcs->dev, "mask field of the property can't be 0\n");
  886. return;
  887. }
  888. value[0] &= value[3];
  889. value[1] &= value[3];
  890. value[2] &= value[3];
  891. ret = pcs_config_match(value[0], value[1], value[2]);
  892. if (ret < 0)
  893. dev_dbg(pcs->dev, "failed to match enable or disable bits\n");
  894. add_config(conf, param, value[0], value[1], value[2], value[3]);
  895. add_setting(settings, param, ret);
  896. }
  897. static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np,
  898. struct pcs_function *func,
  899. struct pinctrl_map **map)
  900. {
  901. struct pinctrl_map *m = *map;
  902. int i = 0, nconfs = 0;
  903. unsigned long *settings = NULL, *s = NULL;
  904. struct pcs_conf_vals *conf = NULL;
  905. struct pcs_conf_type prop2[] = {
  906. { "pinctrl-single,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, },
  907. { "pinctrl-single,slew-rate", PIN_CONFIG_SLEW_RATE, },
  908. { "pinctrl-single,input-schmitt", PIN_CONFIG_INPUT_SCHMITT, },
  909. };
  910. struct pcs_conf_type prop4[] = {
  911. { "pinctrl-single,bias-pullup", PIN_CONFIG_BIAS_PULL_UP, },
  912. { "pinctrl-single,bias-pulldown", PIN_CONFIG_BIAS_PULL_DOWN, },
  913. { "pinctrl-single,input-schmitt-enable",
  914. PIN_CONFIG_INPUT_SCHMITT_ENABLE, },
  915. };
  916. /* If pinconf isn't supported, don't parse properties in below. */
  917. if (!pcs->is_pinconf)
  918. return 0;
  919. /* cacluate how much properties are supported in current node */
  920. for (i = 0; i < ARRAY_SIZE(prop2); i++) {
  921. if (of_find_property(np, prop2[i].name, NULL))
  922. nconfs++;
  923. }
  924. for (i = 0; i < ARRAY_SIZE(prop4); i++) {
  925. if (of_find_property(np, prop4[i].name, NULL))
  926. nconfs++;
  927. }
  928. if (!nconfs)
  929. return 0;
  930. func->conf = devm_kzalloc(pcs->dev,
  931. sizeof(struct pcs_conf_vals) * nconfs,
  932. GFP_KERNEL);
  933. if (!func->conf)
  934. return -ENOMEM;
  935. func->nconfs = nconfs;
  936. conf = &(func->conf[0]);
  937. m++;
  938. settings = devm_kzalloc(pcs->dev, sizeof(unsigned long) * nconfs,
  939. GFP_KERNEL);
  940. if (!settings)
  941. return -ENOMEM;
  942. s = &settings[0];
  943. for (i = 0; i < ARRAY_SIZE(prop2); i++)
  944. pcs_add_conf2(pcs, np, prop2[i].name, prop2[i].param,
  945. &conf, &s);
  946. for (i = 0; i < ARRAY_SIZE(prop4); i++)
  947. pcs_add_conf4(pcs, np, prop4[i].name, prop4[i].param,
  948. &conf, &s);
  949. m->type = PIN_MAP_TYPE_CONFIGS_GROUP;
  950. m->data.configs.group_or_pin = np->name;
  951. m->data.configs.configs = settings;
  952. m->data.configs.num_configs = nconfs;
  953. return 0;
  954. }
  955. static void pcs_free_pingroups(struct pcs_device *pcs);
  956. /**
  957. * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
  958. * @pcs: pinctrl driver instance
  959. * @np: device node of the mux entry
  960. * @map: map entry
  961. * @num_maps: number of map
  962. * @pgnames: pingroup names
  963. *
  964. * Note that this binding currently supports only sets of one register + value.
  965. *
  966. * Also note that this driver tries to avoid understanding pin and function
  967. * names because of the extra bloat they would cause especially in the case of
  968. * a large number of pins. This driver just sets what is specified for the board
  969. * in the .dts file. Further user space debugging tools can be developed to
  970. * decipher the pin and function names using debugfs.
  971. *
  972. * If you are concerned about the boot time, set up the static pins in
  973. * the bootloader, and only set up selected pins as device tree entries.
  974. */
  975. static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
  976. struct device_node *np,
  977. struct pinctrl_map **map,
  978. unsigned *num_maps,
  979. const char **pgnames)
  980. {
  981. struct pcs_func_vals *vals;
  982. const __be32 *mux;
  983. int size, rows, *pins, index = 0, found = 0, res = -ENOMEM;
  984. struct pcs_function *function;
  985. mux = of_get_property(np, PCS_MUX_PINS_NAME, &size);
  986. if ((!mux) || (size < sizeof(*mux) * 2)) {
  987. dev_err(pcs->dev, "bad data for mux %s\n",
  988. np->name);
  989. return -EINVAL;
  990. }
  991. size /= sizeof(*mux); /* Number of elements in array */
  992. rows = size / 2;
  993. vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL);
  994. if (!vals)
  995. return -ENOMEM;
  996. pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL);
  997. if (!pins)
  998. goto free_vals;
  999. while (index < size) {
  1000. unsigned offset, val;
  1001. int pin;
  1002. offset = be32_to_cpup(mux + index++);
  1003. val = be32_to_cpup(mux + index++);
  1004. vals[found].reg = pcs->base + offset;
  1005. vals[found].val = val;
  1006. pin = pcs_get_pin_by_offset(pcs, offset);
  1007. if (pin < 0) {
  1008. dev_err(pcs->dev,
  1009. "could not add functions for %s %ux\n",
  1010. np->name, offset);
  1011. break;
  1012. }
  1013. pins[found++] = pin;
  1014. }
  1015. pgnames[0] = np->name;
  1016. function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
  1017. if (!function)
  1018. goto free_pins;
  1019. res = pcs_add_pingroup(pcs, np, np->name, pins, found);
  1020. if (res < 0)
  1021. goto free_function;
  1022. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  1023. (*map)->data.mux.group = np->name;
  1024. (*map)->data.mux.function = np->name;
  1025. if (pcs->is_pinconf) {
  1026. res = pcs_parse_pinconf(pcs, np, function, map);
  1027. if (res)
  1028. goto free_pingroups;
  1029. *num_maps = 2;
  1030. } else {
  1031. *num_maps = 1;
  1032. }
  1033. return 0;
  1034. free_pingroups:
  1035. pcs_free_pingroups(pcs);
  1036. *num_maps = 1;
  1037. free_function:
  1038. pcs_remove_function(pcs, function);
  1039. free_pins:
  1040. devm_kfree(pcs->dev, pins);
  1041. free_vals:
  1042. devm_kfree(pcs->dev, vals);
  1043. return res;
  1044. }
  1045. #define PARAMS_FOR_BITS_PER_MUX 3
  1046. static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
  1047. struct device_node *np,
  1048. struct pinctrl_map **map,
  1049. unsigned *num_maps,
  1050. const char **pgnames)
  1051. {
  1052. struct pcs_func_vals *vals;
  1053. const __be32 *mux;
  1054. int size, rows, *pins, index = 0, found = 0, res = -ENOMEM;
  1055. int npins_in_row;
  1056. struct pcs_function *function;
  1057. mux = of_get_property(np, PCS_MUX_BITS_NAME, &size);
  1058. if (!mux) {
  1059. dev_err(pcs->dev, "no valid property for %s\n", np->name);
  1060. return -EINVAL;
  1061. }
  1062. if (size < (sizeof(*mux) * PARAMS_FOR_BITS_PER_MUX)) {
  1063. dev_err(pcs->dev, "bad data for %s\n", np->name);
  1064. return -EINVAL;
  1065. }
  1066. /* Number of elements in array */
  1067. size /= sizeof(*mux);
  1068. rows = size / PARAMS_FOR_BITS_PER_MUX;
  1069. npins_in_row = pcs->width / pcs->bits_per_pin;
  1070. vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows * npins_in_row,
  1071. GFP_KERNEL);
  1072. if (!vals)
  1073. return -ENOMEM;
  1074. pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows * npins_in_row,
  1075. GFP_KERNEL);
  1076. if (!pins)
  1077. goto free_vals;
  1078. while (index < size) {
  1079. unsigned offset, val;
  1080. unsigned mask, bit_pos, val_pos, mask_pos, submask;
  1081. unsigned pin_num_from_lsb;
  1082. int pin;
  1083. offset = be32_to_cpup(mux + index++);
  1084. val = be32_to_cpup(mux + index++);
  1085. mask = be32_to_cpup(mux + index++);
  1086. /* Parse pins in each row from LSB */
  1087. while (mask) {
  1088. bit_pos = ffs(mask);
  1089. pin_num_from_lsb = bit_pos / pcs->bits_per_pin;
  1090. mask_pos = ((pcs->fmask) << (bit_pos - 1));
  1091. val_pos = val & mask_pos;
  1092. submask = mask & mask_pos;
  1093. mask &= ~mask_pos;
  1094. if (submask != mask_pos) {
  1095. dev_warn(pcs->dev,
  1096. "Invalid submask 0x%x for %s at 0x%x\n",
  1097. submask, np->name, offset);
  1098. continue;
  1099. }
  1100. vals[found].mask = submask;
  1101. vals[found].reg = pcs->base + offset;
  1102. vals[found].val = val_pos;
  1103. pin = pcs_get_pin_by_offset(pcs, offset);
  1104. if (pin < 0) {
  1105. dev_err(pcs->dev,
  1106. "could not add functions for %s %ux\n",
  1107. np->name, offset);
  1108. break;
  1109. }
  1110. pins[found++] = pin + pin_num_from_lsb;
  1111. }
  1112. }
  1113. pgnames[0] = np->name;
  1114. function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
  1115. if (!function)
  1116. goto free_pins;
  1117. res = pcs_add_pingroup(pcs, np, np->name, pins, found);
  1118. if (res < 0)
  1119. goto free_function;
  1120. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  1121. (*map)->data.mux.group = np->name;
  1122. (*map)->data.mux.function = np->name;
  1123. if (pcs->is_pinconf) {
  1124. dev_err(pcs->dev, "pinconf not supported\n");
  1125. goto free_pingroups;
  1126. }
  1127. *num_maps = 1;
  1128. return 0;
  1129. free_pingroups:
  1130. pcs_free_pingroups(pcs);
  1131. *num_maps = 1;
  1132. free_function:
  1133. pcs_remove_function(pcs, function);
  1134. free_pins:
  1135. devm_kfree(pcs->dev, pins);
  1136. free_vals:
  1137. devm_kfree(pcs->dev, vals);
  1138. return res;
  1139. }
  1140. /**
  1141. * pcs_dt_node_to_map() - allocates and parses pinctrl maps
  1142. * @pctldev: pinctrl instance
  1143. * @np_config: device tree pinmux entry
  1144. * @map: array of map entries
  1145. * @num_maps: number of maps
  1146. */
  1147. static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
  1148. struct device_node *np_config,
  1149. struct pinctrl_map **map, unsigned *num_maps)
  1150. {
  1151. struct pcs_device *pcs;
  1152. const char **pgnames;
  1153. int ret;
  1154. pcs = pinctrl_dev_get_drvdata(pctldev);
  1155. /* create 2 maps. One is for pinmux, and the other is for pinconf. */
  1156. *map = devm_kzalloc(pcs->dev, sizeof(**map) * 2, GFP_KERNEL);
  1157. if (!*map)
  1158. return -ENOMEM;
  1159. *num_maps = 0;
  1160. pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL);
  1161. if (!pgnames) {
  1162. ret = -ENOMEM;
  1163. goto free_map;
  1164. }
  1165. if (pcs->bits_per_mux) {
  1166. ret = pcs_parse_bits_in_pinctrl_entry(pcs, np_config, map,
  1167. num_maps, pgnames);
  1168. if (ret < 0) {
  1169. dev_err(pcs->dev, "no pins entries for %s\n",
  1170. np_config->name);
  1171. goto free_pgnames;
  1172. }
  1173. } else {
  1174. ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map,
  1175. num_maps, pgnames);
  1176. if (ret < 0) {
  1177. dev_err(pcs->dev, "no pins entries for %s\n",
  1178. np_config->name);
  1179. goto free_pgnames;
  1180. }
  1181. }
  1182. return 0;
  1183. free_pgnames:
  1184. devm_kfree(pcs->dev, pgnames);
  1185. free_map:
  1186. devm_kfree(pcs->dev, *map);
  1187. return ret;
  1188. }
  1189. /**
  1190. * pcs_free_funcs() - free memory used by functions
  1191. * @pcs: pcs driver instance
  1192. */
  1193. static void pcs_free_funcs(struct pcs_device *pcs)
  1194. {
  1195. struct list_head *pos, *tmp;
  1196. int i;
  1197. mutex_lock(&pcs->mutex);
  1198. for (i = 0; i < pcs->nfuncs; i++) {
  1199. struct pcs_function *func;
  1200. func = radix_tree_lookup(&pcs->ftree, i);
  1201. if (!func)
  1202. continue;
  1203. radix_tree_delete(&pcs->ftree, i);
  1204. }
  1205. list_for_each_safe(pos, tmp, &pcs->functions) {
  1206. struct pcs_function *function;
  1207. function = list_entry(pos, struct pcs_function, node);
  1208. list_del(&function->node);
  1209. }
  1210. mutex_unlock(&pcs->mutex);
  1211. }
  1212. /**
  1213. * pcs_free_pingroups() - free memory used by pingroups
  1214. * @pcs: pcs driver instance
  1215. */
  1216. static void pcs_free_pingroups(struct pcs_device *pcs)
  1217. {
  1218. struct list_head *pos, *tmp;
  1219. int i;
  1220. mutex_lock(&pcs->mutex);
  1221. for (i = 0; i < pcs->ngroups; i++) {
  1222. struct pcs_pingroup *pingroup;
  1223. pingroup = radix_tree_lookup(&pcs->pgtree, i);
  1224. if (!pingroup)
  1225. continue;
  1226. radix_tree_delete(&pcs->pgtree, i);
  1227. }
  1228. list_for_each_safe(pos, tmp, &pcs->pingroups) {
  1229. struct pcs_pingroup *pingroup;
  1230. pingroup = list_entry(pos, struct pcs_pingroup, node);
  1231. list_del(&pingroup->node);
  1232. }
  1233. mutex_unlock(&pcs->mutex);
  1234. }
  1235. /**
  1236. * pcs_free_resources() - free memory used by this driver
  1237. * @pcs: pcs driver instance
  1238. */
  1239. static void pcs_free_resources(struct pcs_device *pcs)
  1240. {
  1241. if (pcs->pctl)
  1242. pinctrl_unregister(pcs->pctl);
  1243. pcs_free_funcs(pcs);
  1244. pcs_free_pingroups(pcs);
  1245. }
  1246. #define PCS_GET_PROP_U32(name, reg, err) \
  1247. do { \
  1248. ret = of_property_read_u32(np, name, reg); \
  1249. if (ret) { \
  1250. dev_err(pcs->dev, err); \
  1251. return ret; \
  1252. } \
  1253. } while (0);
  1254. static struct of_device_id pcs_of_match[];
  1255. static int pcs_add_gpio_func(struct device_node *node, struct pcs_device *pcs)
  1256. {
  1257. const char *propname = "pinctrl-single,gpio-range";
  1258. const char *cellname = "#pinctrl-single,gpio-range-cells";
  1259. struct of_phandle_args gpiospec;
  1260. struct pcs_gpiofunc_range *range;
  1261. int ret, i;
  1262. for (i = 0; ; i++) {
  1263. ret = of_parse_phandle_with_args(node, propname, cellname,
  1264. i, &gpiospec);
  1265. /* Do not treat it as error. Only treat it as end condition. */
  1266. if (ret) {
  1267. ret = 0;
  1268. break;
  1269. }
  1270. range = devm_kzalloc(pcs->dev, sizeof(*range), GFP_KERNEL);
  1271. if (!range) {
  1272. ret = -ENOMEM;
  1273. break;
  1274. }
  1275. range->offset = gpiospec.args[0];
  1276. range->npins = gpiospec.args[1];
  1277. range->gpiofunc = gpiospec.args[2];
  1278. mutex_lock(&pcs->mutex);
  1279. list_add_tail(&range->node, &pcs->gpiofuncs);
  1280. mutex_unlock(&pcs->mutex);
  1281. }
  1282. return ret;
  1283. }
  1284. static int pcs_probe(struct platform_device *pdev)
  1285. {
  1286. struct device_node *np = pdev->dev.of_node;
  1287. const struct of_device_id *match;
  1288. struct resource *res;
  1289. struct pcs_device *pcs;
  1290. int ret;
  1291. match = of_match_device(pcs_of_match, &pdev->dev);
  1292. if (!match)
  1293. return -EINVAL;
  1294. pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL);
  1295. if (!pcs) {
  1296. dev_err(&pdev->dev, "could not allocate\n");
  1297. return -ENOMEM;
  1298. }
  1299. pcs->dev = &pdev->dev;
  1300. mutex_init(&pcs->mutex);
  1301. INIT_LIST_HEAD(&pcs->pingroups);
  1302. INIT_LIST_HEAD(&pcs->functions);
  1303. INIT_LIST_HEAD(&pcs->gpiofuncs);
  1304. pcs->is_pinconf = match->data;
  1305. PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs->width,
  1306. "register width not specified\n");
  1307. ret = of_property_read_u32(np, "pinctrl-single,function-mask",
  1308. &pcs->fmask);
  1309. if (!ret) {
  1310. pcs->fshift = ffs(pcs->fmask) - 1;
  1311. pcs->fmax = pcs->fmask >> pcs->fshift;
  1312. } else {
  1313. /* If mask property doesn't exist, function mux is invalid. */
  1314. pcs->fmask = 0;
  1315. pcs->fshift = 0;
  1316. pcs->fmax = 0;
  1317. }
  1318. ret = of_property_read_u32(np, "pinctrl-single,function-off",
  1319. &pcs->foff);
  1320. if (ret)
  1321. pcs->foff = PCS_OFF_DISABLED;
  1322. pcs->bits_per_mux = of_property_read_bool(np,
  1323. "pinctrl-single,bit-per-mux");
  1324. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1325. if (!res) {
  1326. dev_err(pcs->dev, "could not get resource\n");
  1327. return -ENODEV;
  1328. }
  1329. pcs->res = devm_request_mem_region(pcs->dev, res->start,
  1330. resource_size(res), DRIVER_NAME);
  1331. if (!pcs->res) {
  1332. dev_err(pcs->dev, "could not get mem_region\n");
  1333. return -EBUSY;
  1334. }
  1335. pcs->size = resource_size(pcs->res);
  1336. pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size);
  1337. if (!pcs->base) {
  1338. dev_err(pcs->dev, "could not ioremap\n");
  1339. return -ENODEV;
  1340. }
  1341. INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL);
  1342. INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL);
  1343. platform_set_drvdata(pdev, pcs);
  1344. switch (pcs->width) {
  1345. case 8:
  1346. pcs->read = pcs_readb;
  1347. pcs->write = pcs_writeb;
  1348. break;
  1349. case 16:
  1350. pcs->read = pcs_readw;
  1351. pcs->write = pcs_writew;
  1352. break;
  1353. case 32:
  1354. pcs->read = pcs_readl;
  1355. pcs->write = pcs_writel;
  1356. break;
  1357. default:
  1358. break;
  1359. }
  1360. pcs->desc.name = DRIVER_NAME;
  1361. pcs->desc.pctlops = &pcs_pinctrl_ops;
  1362. pcs->desc.pmxops = &pcs_pinmux_ops;
  1363. if (pcs->is_pinconf)
  1364. pcs->desc.confops = &pcs_pinconf_ops;
  1365. pcs->desc.owner = THIS_MODULE;
  1366. ret = pcs_allocate_pin_table(pcs);
  1367. if (ret < 0)
  1368. goto free;
  1369. pcs->pctl = pinctrl_register(&pcs->desc, pcs->dev, pcs);
  1370. if (!pcs->pctl) {
  1371. dev_err(pcs->dev, "could not register single pinctrl driver\n");
  1372. ret = -EINVAL;
  1373. goto free;
  1374. }
  1375. ret = pcs_add_gpio_func(np, pcs);
  1376. if (ret < 0)
  1377. goto free;
  1378. dev_info(pcs->dev, "%i pins at pa %p size %u\n",
  1379. pcs->desc.npins, pcs->base, pcs->size);
  1380. return 0;
  1381. free:
  1382. pcs_free_resources(pcs);
  1383. return ret;
  1384. }
  1385. static int pcs_remove(struct platform_device *pdev)
  1386. {
  1387. struct pcs_device *pcs = platform_get_drvdata(pdev);
  1388. if (!pcs)
  1389. return 0;
  1390. pcs_free_resources(pcs);
  1391. return 0;
  1392. }
  1393. static struct of_device_id pcs_of_match[] = {
  1394. { .compatible = "pinctrl-single", .data = (void *)false },
  1395. { .compatible = "pinconf-single", .data = (void *)true },
  1396. { },
  1397. };
  1398. MODULE_DEVICE_TABLE(of, pcs_of_match);
  1399. static struct platform_driver pcs_driver = {
  1400. .probe = pcs_probe,
  1401. .remove = pcs_remove,
  1402. .driver = {
  1403. .owner = THIS_MODULE,
  1404. .name = DRIVER_NAME,
  1405. .of_match_table = pcs_of_match,
  1406. },
  1407. };
  1408. module_platform_driver(pcs_driver);
  1409. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  1410. MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver");
  1411. MODULE_LICENSE("GPL v2");