pinctrl-single.c 36 KB

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