pinctrl-single.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. /*
  2. * Generic device tree based pinctrl driver for one register per pin
  3. * type pinmux controllers
  4. *
  5. * Copyright (C) 2012 Texas Instruments, Inc.
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/io.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/list.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_address.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/pinctrl/pinconf-generic.h>
  23. #include "core.h"
  24. #include "pinconf.h"
  25. #define DRIVER_NAME "pinctrl-single"
  26. #define PCS_MUX_PINS_NAME "pinctrl-single,pins"
  27. #define PCS_MUX_BITS_NAME "pinctrl-single,bits"
  28. #define PCS_REG_NAME_LEN ((sizeof(unsigned long) * 2) + 3)
  29. #define PCS_OFF_DISABLED ~0U
  30. /**
  31. * struct pcs_pingroup - pingroups for a function
  32. * @np: pingroup device node pointer
  33. * @name: pingroup name
  34. * @gpins: array of the pins in the group
  35. * @ngpins: number of pins in the group
  36. * @node: list node
  37. */
  38. struct pcs_pingroup {
  39. struct device_node *np;
  40. const char *name;
  41. int *gpins;
  42. int ngpins;
  43. struct list_head node;
  44. };
  45. /**
  46. * struct pcs_func_vals - mux function register offset and value pair
  47. * @reg: register virtual address
  48. * @val: register value
  49. */
  50. struct pcs_func_vals {
  51. void __iomem *reg;
  52. unsigned val;
  53. unsigned mask;
  54. };
  55. /**
  56. * struct pcs_conf_vals - pinconf parameter, pinconf register offset
  57. * and value, enable, disable, mask
  58. * @param: config parameter
  59. * @val: user input bits in the pinconf register
  60. * @enable: enable bits in the pinconf register
  61. * @disable: disable bits in the pinconf register
  62. * @mask: mask bits in the register value
  63. */
  64. struct pcs_conf_vals {
  65. enum pin_config_param param;
  66. unsigned val;
  67. unsigned enable;
  68. unsigned disable;
  69. unsigned mask;
  70. };
  71. /**
  72. * struct pcs_conf_type - pinconf property name, pinconf param pair
  73. * @name: property name in DTS file
  74. * @param: config parameter
  75. */
  76. struct pcs_conf_type {
  77. const char *name;
  78. enum pin_config_param param;
  79. };
  80. /**
  81. * struct pcs_function - pinctrl function
  82. * @name: pinctrl function name
  83. * @vals: register and vals array
  84. * @nvals: number of entries in vals array
  85. * @pgnames: array of pingroup names the function uses
  86. * @npgnames: number of pingroup names the function uses
  87. * @node: list node
  88. */
  89. struct pcs_function {
  90. const char *name;
  91. struct pcs_func_vals *vals;
  92. unsigned nvals;
  93. const char **pgnames;
  94. int npgnames;
  95. struct pcs_conf_vals *conf;
  96. int nconfs;
  97. struct list_head node;
  98. };
  99. /**
  100. * struct pcs_gpiofunc_range - pin ranges with same mux value of gpio function
  101. * @offset: offset base of pins
  102. * @npins: number pins with the same mux value of gpio function
  103. * @gpiofunc: mux value of gpio function
  104. * @node: list node
  105. */
  106. struct pcs_gpiofunc_range {
  107. unsigned offset;
  108. unsigned npins;
  109. unsigned gpiofunc;
  110. struct list_head node;
  111. };
  112. /**
  113. * struct pcs_data - wrapper for data needed by pinctrl framework
  114. * @pa: pindesc array
  115. * @cur: index to current element
  116. *
  117. * REVISIT: We should be able to drop this eventually by adding
  118. * support for registering pins individually in the pinctrl
  119. * framework for those drivers that don't need a static array.
  120. */
  121. struct pcs_data {
  122. struct pinctrl_pin_desc *pa;
  123. int cur;
  124. };
  125. /**
  126. * struct pcs_name - register name for a pin
  127. * @name: name of the pinctrl register
  128. *
  129. * REVISIT: We may want to make names optional in the pinctrl
  130. * framework as some drivers may not care about pin names to
  131. * avoid kernel bloat. The pin names can be deciphered by user
  132. * space tools using debugfs based on the register address and
  133. * SoC packaging information.
  134. */
  135. struct pcs_name {
  136. char name[PCS_REG_NAME_LEN];
  137. };
  138. /**
  139. * struct pcs_device - pinctrl device instance
  140. * @res: resources
  141. * @base: virtual address of the controller
  142. * @size: size of the ioremapped area
  143. * @dev: device entry
  144. * @pctl: pin controller device
  145. * @mutex: mutex protecting the lists
  146. * @width: bits per mux register
  147. * @fmask: function register mask
  148. * @fshift: function register shift
  149. * @foff: value to turn mux off
  150. * @fmax: max number of functions in fmask
  151. * @is_pinconf: whether supports pinconf
  152. * @bits_per_pin:number of bits per pin
  153. * @names: array of register names for pins
  154. * @pins: physical pins on the SoC
  155. * @pgtree: pingroup index radix tree
  156. * @ftree: function index radix tree
  157. * @pingroups: list of pingroups
  158. * @functions: list of functions
  159. * @gpiofuncs: list of gpio functions
  160. * @ngroups: number of pingroups
  161. * @nfuncs: number of functions
  162. * @desc: pin controller descriptor
  163. * @read: register read function to use
  164. * @write: register write function to use
  165. */
  166. struct pcs_device {
  167. struct resource *res;
  168. void __iomem *base;
  169. unsigned size;
  170. struct device *dev;
  171. struct pinctrl_dev *pctl;
  172. struct mutex mutex;
  173. unsigned width;
  174. unsigned fmask;
  175. unsigned fshift;
  176. unsigned foff;
  177. unsigned fmax;
  178. bool bits_per_mux;
  179. bool is_pinconf;
  180. unsigned bits_per_pin;
  181. struct pcs_name *names;
  182. struct pcs_data pins;
  183. struct radix_tree_root pgtree;
  184. struct radix_tree_root ftree;
  185. struct list_head pingroups;
  186. struct list_head functions;
  187. struct list_head gpiofuncs;
  188. unsigned ngroups;
  189. unsigned nfuncs;
  190. struct pinctrl_desc desc;
  191. unsigned (*read)(void __iomem *reg);
  192. void (*write)(unsigned val, void __iomem *reg);
  193. };
  194. static int pcs_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
  195. unsigned long *config);
  196. static int pcs_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
  197. unsigned long *configs, unsigned num_configs);
  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, 1);
  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 *configs,
  547. unsigned num_configs)
  548. {
  549. struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
  550. struct pcs_function *func;
  551. unsigned offset = 0, shift = 0, i, data, ret;
  552. u16 arg;
  553. int j;
  554. ret = pcs_get_function(pctldev, pin, &func);
  555. if (ret)
  556. return ret;
  557. for (j = 0; j < num_configs; j++) {
  558. for (i = 0; i < func->nconfs; i++) {
  559. if (pinconf_to_config_param(configs[j])
  560. != func->conf[i].param)
  561. continue;
  562. offset = pin * (pcs->width / BITS_PER_BYTE);
  563. data = pcs->read(pcs->base + offset);
  564. arg = pinconf_to_config_argument(configs[j]);
  565. switch (func->conf[i].param) {
  566. /* 2 parameters */
  567. case PIN_CONFIG_INPUT_SCHMITT:
  568. case PIN_CONFIG_DRIVE_STRENGTH:
  569. case PIN_CONFIG_SLEW_RATE:
  570. shift = ffs(func->conf[i].mask) - 1;
  571. data &= ~func->conf[i].mask;
  572. data |= (arg << shift) & func->conf[i].mask;
  573. break;
  574. /* 4 parameters */
  575. case PIN_CONFIG_BIAS_DISABLE:
  576. pcs_pinconf_clear_bias(pctldev, pin);
  577. break;
  578. case PIN_CONFIG_BIAS_PULL_DOWN:
  579. case PIN_CONFIG_BIAS_PULL_UP:
  580. if (arg)
  581. pcs_pinconf_clear_bias(pctldev, pin);
  582. /* fall through */
  583. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  584. data &= ~func->conf[i].mask;
  585. if (arg)
  586. data |= func->conf[i].enable;
  587. else
  588. data |= func->conf[i].disable;
  589. break;
  590. default:
  591. return -ENOTSUPP;
  592. }
  593. pcs->write(data, pcs->base + offset);
  594. break;
  595. }
  596. if (i >= func->nconfs)
  597. return -ENOTSUPP;
  598. } /* for each config */
  599. return 0;
  600. }
  601. static int pcs_pinconf_group_get(struct pinctrl_dev *pctldev,
  602. unsigned group, unsigned long *config)
  603. {
  604. const unsigned *pins;
  605. unsigned npins, old = 0;
  606. int i, ret;
  607. ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
  608. if (ret)
  609. return ret;
  610. for (i = 0; i < npins; i++) {
  611. if (pcs_pinconf_get(pctldev, pins[i], config))
  612. return -ENOTSUPP;
  613. /* configs do not match between two pins */
  614. if (i && (old != *config))
  615. return -ENOTSUPP;
  616. old = *config;
  617. }
  618. return 0;
  619. }
  620. static int pcs_pinconf_group_set(struct pinctrl_dev *pctldev,
  621. unsigned group, unsigned long *configs,
  622. unsigned num_configs)
  623. {
  624. const unsigned *pins;
  625. unsigned npins;
  626. int i, ret;
  627. ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
  628. if (ret)
  629. return ret;
  630. for (i = 0; i < npins; i++) {
  631. if (pcs_pinconf_set(pctldev, pins[i], configs, num_configs))
  632. return -ENOTSUPP;
  633. }
  634. return 0;
  635. }
  636. static void pcs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  637. struct seq_file *s, unsigned pin)
  638. {
  639. }
  640. static void pcs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  641. struct seq_file *s, unsigned selector)
  642. {
  643. }
  644. static void pcs_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
  645. struct seq_file *s,
  646. unsigned long config)
  647. {
  648. pinconf_generic_dump_config(pctldev, s, config);
  649. }
  650. static const struct pinconf_ops pcs_pinconf_ops = {
  651. .pin_config_get = pcs_pinconf_get,
  652. .pin_config_set = pcs_pinconf_set,
  653. .pin_config_group_get = pcs_pinconf_group_get,
  654. .pin_config_group_set = pcs_pinconf_group_set,
  655. .pin_config_dbg_show = pcs_pinconf_dbg_show,
  656. .pin_config_group_dbg_show = pcs_pinconf_group_dbg_show,
  657. .pin_config_config_dbg_show = pcs_pinconf_config_dbg_show,
  658. .is_generic = true,
  659. };
  660. /**
  661. * pcs_add_pin() - add a pin to the static per controller pin array
  662. * @pcs: pcs driver instance
  663. * @offset: register offset from base
  664. */
  665. static int pcs_add_pin(struct pcs_device *pcs, unsigned offset,
  666. unsigned pin_pos)
  667. {
  668. struct pinctrl_pin_desc *pin;
  669. struct pcs_name *pn;
  670. int i;
  671. i = pcs->pins.cur;
  672. if (i >= pcs->desc.npins) {
  673. dev_err(pcs->dev, "too many pins, max %i\n",
  674. pcs->desc.npins);
  675. return -ENOMEM;
  676. }
  677. pin = &pcs->pins.pa[i];
  678. pn = &pcs->names[i];
  679. sprintf(pn->name, "%lx.%d",
  680. (unsigned long)pcs->res->start + offset, pin_pos);
  681. pin->name = pn->name;
  682. pin->number = i;
  683. pcs->pins.cur++;
  684. return i;
  685. }
  686. /**
  687. * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver
  688. * @pcs: pcs driver instance
  689. *
  690. * In case of errors, resources are freed in pcs_free_resources.
  691. *
  692. * If your hardware needs holes in the address space, then just set
  693. * up multiple driver instances.
  694. */
  695. static int pcs_allocate_pin_table(struct pcs_device *pcs)
  696. {
  697. int mux_bytes, nr_pins, i;
  698. int num_pins_in_register = 0;
  699. mux_bytes = pcs->width / BITS_PER_BYTE;
  700. if (pcs->bits_per_mux) {
  701. pcs->bits_per_pin = fls(pcs->fmask);
  702. nr_pins = (pcs->size * BITS_PER_BYTE) / pcs->bits_per_pin;
  703. num_pins_in_register = pcs->width / pcs->bits_per_pin;
  704. } else {
  705. nr_pins = pcs->size / mux_bytes;
  706. }
  707. dev_dbg(pcs->dev, "allocating %i pins\n", nr_pins);
  708. pcs->pins.pa = devm_kzalloc(pcs->dev,
  709. sizeof(*pcs->pins.pa) * nr_pins,
  710. GFP_KERNEL);
  711. if (!pcs->pins.pa)
  712. return -ENOMEM;
  713. pcs->names = devm_kzalloc(pcs->dev,
  714. sizeof(struct pcs_name) * nr_pins,
  715. GFP_KERNEL);
  716. if (!pcs->names)
  717. return -ENOMEM;
  718. pcs->desc.pins = pcs->pins.pa;
  719. pcs->desc.npins = nr_pins;
  720. for (i = 0; i < pcs->desc.npins; i++) {
  721. unsigned offset;
  722. int res;
  723. int byte_num;
  724. int pin_pos = 0;
  725. if (pcs->bits_per_mux) {
  726. byte_num = (pcs->bits_per_pin * i) / BITS_PER_BYTE;
  727. offset = (byte_num / mux_bytes) * mux_bytes;
  728. pin_pos = i % num_pins_in_register;
  729. } else {
  730. offset = i * mux_bytes;
  731. }
  732. res = pcs_add_pin(pcs, offset, pin_pos);
  733. if (res < 0) {
  734. dev_err(pcs->dev, "error adding pins: %i\n", res);
  735. return res;
  736. }
  737. }
  738. return 0;
  739. }
  740. /**
  741. * pcs_add_function() - adds a new function to the function list
  742. * @pcs: pcs driver instance
  743. * @np: device node of the mux entry
  744. * @name: name of the function
  745. * @vals: array of mux register value pairs used by the function
  746. * @nvals: number of mux register value pairs
  747. * @pgnames: array of pingroup names for the function
  748. * @npgnames: number of pingroup names
  749. */
  750. static struct pcs_function *pcs_add_function(struct pcs_device *pcs,
  751. struct device_node *np,
  752. const char *name,
  753. struct pcs_func_vals *vals,
  754. unsigned nvals,
  755. const char **pgnames,
  756. unsigned npgnames)
  757. {
  758. struct pcs_function *function;
  759. function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL);
  760. if (!function)
  761. return NULL;
  762. function->name = name;
  763. function->vals = vals;
  764. function->nvals = nvals;
  765. function->pgnames = pgnames;
  766. function->npgnames = npgnames;
  767. mutex_lock(&pcs->mutex);
  768. list_add_tail(&function->node, &pcs->functions);
  769. radix_tree_insert(&pcs->ftree, pcs->nfuncs, function);
  770. pcs->nfuncs++;
  771. mutex_unlock(&pcs->mutex);
  772. return function;
  773. }
  774. static void pcs_remove_function(struct pcs_device *pcs,
  775. struct pcs_function *function)
  776. {
  777. int i;
  778. mutex_lock(&pcs->mutex);
  779. for (i = 0; i < pcs->nfuncs; i++) {
  780. struct pcs_function *found;
  781. found = radix_tree_lookup(&pcs->ftree, i);
  782. if (found == function)
  783. radix_tree_delete(&pcs->ftree, i);
  784. }
  785. list_del(&function->node);
  786. mutex_unlock(&pcs->mutex);
  787. }
  788. /**
  789. * pcs_add_pingroup() - add a pingroup to the pingroup list
  790. * @pcs: pcs driver instance
  791. * @np: device node of the mux entry
  792. * @name: name of the pingroup
  793. * @gpins: array of the pins that belong to the group
  794. * @ngpins: number of pins in the group
  795. */
  796. static int pcs_add_pingroup(struct pcs_device *pcs,
  797. struct device_node *np,
  798. const char *name,
  799. int *gpins,
  800. int ngpins)
  801. {
  802. struct pcs_pingroup *pingroup;
  803. pingroup = devm_kzalloc(pcs->dev, sizeof(*pingroup), GFP_KERNEL);
  804. if (!pingroup)
  805. return -ENOMEM;
  806. pingroup->name = name;
  807. pingroup->np = np;
  808. pingroup->gpins = gpins;
  809. pingroup->ngpins = ngpins;
  810. mutex_lock(&pcs->mutex);
  811. list_add_tail(&pingroup->node, &pcs->pingroups);
  812. radix_tree_insert(&pcs->pgtree, pcs->ngroups, pingroup);
  813. pcs->ngroups++;
  814. mutex_unlock(&pcs->mutex);
  815. return 0;
  816. }
  817. /**
  818. * pcs_get_pin_by_offset() - get a pin index based on the register offset
  819. * @pcs: pcs driver instance
  820. * @offset: register offset from the base
  821. *
  822. * Note that this is OK as long as the pins are in a static array.
  823. */
  824. static int pcs_get_pin_by_offset(struct pcs_device *pcs, unsigned offset)
  825. {
  826. unsigned index;
  827. if (offset >= pcs->size) {
  828. dev_err(pcs->dev, "mux offset out of range: 0x%x (0x%x)\n",
  829. offset, pcs->size);
  830. return -EINVAL;
  831. }
  832. if (pcs->bits_per_mux)
  833. index = (offset * BITS_PER_BYTE) / pcs->bits_per_pin;
  834. else
  835. index = offset / (pcs->width / BITS_PER_BYTE);
  836. return index;
  837. }
  838. /*
  839. * check whether data matches enable bits or disable bits
  840. * Return value: 1 for matching enable bits, 0 for matching disable bits,
  841. * and negative value for matching failure.
  842. */
  843. static int pcs_config_match(unsigned data, unsigned enable, unsigned disable)
  844. {
  845. int ret = -EINVAL;
  846. if (data == enable)
  847. ret = 1;
  848. else if (data == disable)
  849. ret = 0;
  850. return ret;
  851. }
  852. static void add_config(struct pcs_conf_vals **conf, enum pin_config_param param,
  853. unsigned value, unsigned enable, unsigned disable,
  854. unsigned mask)
  855. {
  856. (*conf)->param = param;
  857. (*conf)->val = value;
  858. (*conf)->enable = enable;
  859. (*conf)->disable = disable;
  860. (*conf)->mask = mask;
  861. (*conf)++;
  862. }
  863. static void add_setting(unsigned long **setting, enum pin_config_param param,
  864. unsigned arg)
  865. {
  866. **setting = pinconf_to_config_packed(param, arg);
  867. (*setting)++;
  868. }
  869. /* add pinconf setting with 2 parameters */
  870. static void pcs_add_conf2(struct pcs_device *pcs, struct device_node *np,
  871. const char *name, enum pin_config_param param,
  872. struct pcs_conf_vals **conf, unsigned long **settings)
  873. {
  874. unsigned value[2], shift;
  875. int ret;
  876. ret = of_property_read_u32_array(np, name, value, 2);
  877. if (ret)
  878. return;
  879. /* set value & mask */
  880. value[0] &= value[1];
  881. shift = ffs(value[1]) - 1;
  882. /* skip enable & disable */
  883. add_config(conf, param, value[0], 0, 0, value[1]);
  884. add_setting(settings, param, value[0] >> shift);
  885. }
  886. /* add pinconf setting with 4 parameters */
  887. static void pcs_add_conf4(struct pcs_device *pcs, struct device_node *np,
  888. const char *name, enum pin_config_param param,
  889. struct pcs_conf_vals **conf, unsigned long **settings)
  890. {
  891. unsigned value[4];
  892. int ret;
  893. /* value to set, enable, disable, mask */
  894. ret = of_property_read_u32_array(np, name, value, 4);
  895. if (ret)
  896. return;
  897. if (!value[3]) {
  898. dev_err(pcs->dev, "mask field of the property can't be 0\n");
  899. return;
  900. }
  901. value[0] &= value[3];
  902. value[1] &= value[3];
  903. value[2] &= value[3];
  904. ret = pcs_config_match(value[0], value[1], value[2]);
  905. if (ret < 0)
  906. dev_dbg(pcs->dev, "failed to match enable or disable bits\n");
  907. add_config(conf, param, value[0], value[1], value[2], value[3]);
  908. add_setting(settings, param, ret);
  909. }
  910. static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np,
  911. struct pcs_function *func,
  912. struct pinctrl_map **map)
  913. {
  914. struct pinctrl_map *m = *map;
  915. int i = 0, nconfs = 0;
  916. unsigned long *settings = NULL, *s = NULL;
  917. struct pcs_conf_vals *conf = NULL;
  918. struct pcs_conf_type prop2[] = {
  919. { "pinctrl-single,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, },
  920. { "pinctrl-single,slew-rate", PIN_CONFIG_SLEW_RATE, },
  921. { "pinctrl-single,input-schmitt", PIN_CONFIG_INPUT_SCHMITT, },
  922. };
  923. struct pcs_conf_type prop4[] = {
  924. { "pinctrl-single,bias-pullup", PIN_CONFIG_BIAS_PULL_UP, },
  925. { "pinctrl-single,bias-pulldown", PIN_CONFIG_BIAS_PULL_DOWN, },
  926. { "pinctrl-single,input-schmitt-enable",
  927. PIN_CONFIG_INPUT_SCHMITT_ENABLE, },
  928. };
  929. /* If pinconf isn't supported, don't parse properties in below. */
  930. if (!pcs->is_pinconf)
  931. return 0;
  932. /* cacluate how much properties are supported in current node */
  933. for (i = 0; i < ARRAY_SIZE(prop2); i++) {
  934. if (of_find_property(np, prop2[i].name, NULL))
  935. nconfs++;
  936. }
  937. for (i = 0; i < ARRAY_SIZE(prop4); i++) {
  938. if (of_find_property(np, prop4[i].name, NULL))
  939. nconfs++;
  940. }
  941. if (!nconfs)
  942. return 0;
  943. func->conf = devm_kzalloc(pcs->dev,
  944. sizeof(struct pcs_conf_vals) * nconfs,
  945. GFP_KERNEL);
  946. if (!func->conf)
  947. return -ENOMEM;
  948. func->nconfs = nconfs;
  949. conf = &(func->conf[0]);
  950. m++;
  951. settings = devm_kzalloc(pcs->dev, sizeof(unsigned long) * nconfs,
  952. GFP_KERNEL);
  953. if (!settings)
  954. return -ENOMEM;
  955. s = &settings[0];
  956. for (i = 0; i < ARRAY_SIZE(prop2); i++)
  957. pcs_add_conf2(pcs, np, prop2[i].name, prop2[i].param,
  958. &conf, &s);
  959. for (i = 0; i < ARRAY_SIZE(prop4); i++)
  960. pcs_add_conf4(pcs, np, prop4[i].name, prop4[i].param,
  961. &conf, &s);
  962. m->type = PIN_MAP_TYPE_CONFIGS_GROUP;
  963. m->data.configs.group_or_pin = np->name;
  964. m->data.configs.configs = settings;
  965. m->data.configs.num_configs = nconfs;
  966. return 0;
  967. }
  968. static void pcs_free_pingroups(struct pcs_device *pcs);
  969. /**
  970. * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
  971. * @pcs: pinctrl driver instance
  972. * @np: device node of the mux entry
  973. * @map: map entry
  974. * @num_maps: number of map
  975. * @pgnames: pingroup names
  976. *
  977. * Note that this binding currently supports only sets of one register + value.
  978. *
  979. * Also note that this driver tries to avoid understanding pin and function
  980. * names because of the extra bloat they would cause especially in the case of
  981. * a large number of pins. This driver just sets what is specified for the board
  982. * in the .dts file. Further user space debugging tools can be developed to
  983. * decipher the pin and function names using debugfs.
  984. *
  985. * If you are concerned about the boot time, set up the static pins in
  986. * the bootloader, and only set up selected pins as device tree entries.
  987. */
  988. static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
  989. struct device_node *np,
  990. struct pinctrl_map **map,
  991. unsigned *num_maps,
  992. const char **pgnames)
  993. {
  994. struct pcs_func_vals *vals;
  995. const __be32 *mux;
  996. int size, rows, *pins, index = 0, found = 0, res = -ENOMEM;
  997. struct pcs_function *function;
  998. mux = of_get_property(np, PCS_MUX_PINS_NAME, &size);
  999. if ((!mux) || (size < sizeof(*mux) * 2)) {
  1000. dev_err(pcs->dev, "bad data for mux %s\n",
  1001. np->name);
  1002. return -EINVAL;
  1003. }
  1004. size /= sizeof(*mux); /* Number of elements in array */
  1005. rows = size / 2;
  1006. vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL);
  1007. if (!vals)
  1008. return -ENOMEM;
  1009. pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL);
  1010. if (!pins)
  1011. goto free_vals;
  1012. while (index < size) {
  1013. unsigned offset, val;
  1014. int pin;
  1015. offset = be32_to_cpup(mux + index++);
  1016. val = be32_to_cpup(mux + index++);
  1017. vals[found].reg = pcs->base + offset;
  1018. vals[found].val = val;
  1019. pin = pcs_get_pin_by_offset(pcs, offset);
  1020. if (pin < 0) {
  1021. dev_err(pcs->dev,
  1022. "could not add functions for %s %ux\n",
  1023. np->name, offset);
  1024. break;
  1025. }
  1026. pins[found++] = pin;
  1027. }
  1028. pgnames[0] = np->name;
  1029. function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
  1030. if (!function)
  1031. goto free_pins;
  1032. res = pcs_add_pingroup(pcs, np, np->name, pins, found);
  1033. if (res < 0)
  1034. goto free_function;
  1035. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  1036. (*map)->data.mux.group = np->name;
  1037. (*map)->data.mux.function = np->name;
  1038. if (pcs->is_pinconf) {
  1039. res = pcs_parse_pinconf(pcs, np, function, map);
  1040. if (res)
  1041. goto free_pingroups;
  1042. *num_maps = 2;
  1043. } else {
  1044. *num_maps = 1;
  1045. }
  1046. return 0;
  1047. free_pingroups:
  1048. pcs_free_pingroups(pcs);
  1049. *num_maps = 1;
  1050. free_function:
  1051. pcs_remove_function(pcs, function);
  1052. free_pins:
  1053. devm_kfree(pcs->dev, pins);
  1054. free_vals:
  1055. devm_kfree(pcs->dev, vals);
  1056. return res;
  1057. }
  1058. #define PARAMS_FOR_BITS_PER_MUX 3
  1059. static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
  1060. struct device_node *np,
  1061. struct pinctrl_map **map,
  1062. unsigned *num_maps,
  1063. const char **pgnames)
  1064. {
  1065. struct pcs_func_vals *vals;
  1066. const __be32 *mux;
  1067. int size, rows, *pins, index = 0, found = 0, res = -ENOMEM;
  1068. int npins_in_row;
  1069. struct pcs_function *function;
  1070. mux = of_get_property(np, PCS_MUX_BITS_NAME, &size);
  1071. if (!mux) {
  1072. dev_err(pcs->dev, "no valid property for %s\n", np->name);
  1073. return -EINVAL;
  1074. }
  1075. if (size < (sizeof(*mux) * PARAMS_FOR_BITS_PER_MUX)) {
  1076. dev_err(pcs->dev, "bad data for %s\n", np->name);
  1077. return -EINVAL;
  1078. }
  1079. /* Number of elements in array */
  1080. size /= sizeof(*mux);
  1081. rows = size / PARAMS_FOR_BITS_PER_MUX;
  1082. npins_in_row = pcs->width / pcs->bits_per_pin;
  1083. vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows * npins_in_row,
  1084. GFP_KERNEL);
  1085. if (!vals)
  1086. return -ENOMEM;
  1087. pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows * npins_in_row,
  1088. GFP_KERNEL);
  1089. if (!pins)
  1090. goto free_vals;
  1091. while (index < size) {
  1092. unsigned offset, val;
  1093. unsigned mask, bit_pos, val_pos, mask_pos, submask;
  1094. unsigned pin_num_from_lsb;
  1095. int pin;
  1096. offset = be32_to_cpup(mux + index++);
  1097. val = be32_to_cpup(mux + index++);
  1098. mask = be32_to_cpup(mux + index++);
  1099. /* Parse pins in each row from LSB */
  1100. while (mask) {
  1101. bit_pos = ffs(mask);
  1102. pin_num_from_lsb = bit_pos / pcs->bits_per_pin;
  1103. mask_pos = ((pcs->fmask) << (bit_pos - 1));
  1104. val_pos = val & mask_pos;
  1105. submask = mask & mask_pos;
  1106. mask &= ~mask_pos;
  1107. if (submask != mask_pos) {
  1108. dev_warn(pcs->dev,
  1109. "Invalid submask 0x%x for %s at 0x%x\n",
  1110. submask, np->name, offset);
  1111. continue;
  1112. }
  1113. vals[found].mask = submask;
  1114. vals[found].reg = pcs->base + offset;
  1115. vals[found].val = val_pos;
  1116. pin = pcs_get_pin_by_offset(pcs, offset);
  1117. if (pin < 0) {
  1118. dev_err(pcs->dev,
  1119. "could not add functions for %s %ux\n",
  1120. np->name, offset);
  1121. break;
  1122. }
  1123. pins[found++] = pin + pin_num_from_lsb;
  1124. }
  1125. }
  1126. pgnames[0] = np->name;
  1127. function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
  1128. if (!function)
  1129. goto free_pins;
  1130. res = pcs_add_pingroup(pcs, np, np->name, pins, found);
  1131. if (res < 0)
  1132. goto free_function;
  1133. (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
  1134. (*map)->data.mux.group = np->name;
  1135. (*map)->data.mux.function = np->name;
  1136. if (pcs->is_pinconf) {
  1137. dev_err(pcs->dev, "pinconf not supported\n");
  1138. goto free_pingroups;
  1139. }
  1140. *num_maps = 1;
  1141. return 0;
  1142. free_pingroups:
  1143. pcs_free_pingroups(pcs);
  1144. *num_maps = 1;
  1145. free_function:
  1146. pcs_remove_function(pcs, function);
  1147. free_pins:
  1148. devm_kfree(pcs->dev, pins);
  1149. free_vals:
  1150. devm_kfree(pcs->dev, vals);
  1151. return res;
  1152. }
  1153. /**
  1154. * pcs_dt_node_to_map() - allocates and parses pinctrl maps
  1155. * @pctldev: pinctrl instance
  1156. * @np_config: device tree pinmux entry
  1157. * @map: array of map entries
  1158. * @num_maps: number of maps
  1159. */
  1160. static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
  1161. struct device_node *np_config,
  1162. struct pinctrl_map **map, unsigned *num_maps)
  1163. {
  1164. struct pcs_device *pcs;
  1165. const char **pgnames;
  1166. int ret;
  1167. pcs = pinctrl_dev_get_drvdata(pctldev);
  1168. /* create 2 maps. One is for pinmux, and the other is for pinconf. */
  1169. *map = devm_kzalloc(pcs->dev, sizeof(**map) * 2, GFP_KERNEL);
  1170. if (!*map)
  1171. return -ENOMEM;
  1172. *num_maps = 0;
  1173. pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL);
  1174. if (!pgnames) {
  1175. ret = -ENOMEM;
  1176. goto free_map;
  1177. }
  1178. if (pcs->bits_per_mux) {
  1179. ret = pcs_parse_bits_in_pinctrl_entry(pcs, np_config, map,
  1180. num_maps, pgnames);
  1181. if (ret < 0) {
  1182. dev_err(pcs->dev, "no pins entries for %s\n",
  1183. np_config->name);
  1184. goto free_pgnames;
  1185. }
  1186. } else {
  1187. ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map,
  1188. num_maps, pgnames);
  1189. if (ret < 0) {
  1190. dev_err(pcs->dev, "no pins entries for %s\n",
  1191. np_config->name);
  1192. goto free_pgnames;
  1193. }
  1194. }
  1195. return 0;
  1196. free_pgnames:
  1197. devm_kfree(pcs->dev, pgnames);
  1198. free_map:
  1199. devm_kfree(pcs->dev, *map);
  1200. return ret;
  1201. }
  1202. /**
  1203. * pcs_free_funcs() - free memory used by functions
  1204. * @pcs: pcs driver instance
  1205. */
  1206. static void pcs_free_funcs(struct pcs_device *pcs)
  1207. {
  1208. struct list_head *pos, *tmp;
  1209. int i;
  1210. mutex_lock(&pcs->mutex);
  1211. for (i = 0; i < pcs->nfuncs; i++) {
  1212. struct pcs_function *func;
  1213. func = radix_tree_lookup(&pcs->ftree, i);
  1214. if (!func)
  1215. continue;
  1216. radix_tree_delete(&pcs->ftree, i);
  1217. }
  1218. list_for_each_safe(pos, tmp, &pcs->functions) {
  1219. struct pcs_function *function;
  1220. function = list_entry(pos, struct pcs_function, node);
  1221. list_del(&function->node);
  1222. }
  1223. mutex_unlock(&pcs->mutex);
  1224. }
  1225. /**
  1226. * pcs_free_pingroups() - free memory used by pingroups
  1227. * @pcs: pcs driver instance
  1228. */
  1229. static void pcs_free_pingroups(struct pcs_device *pcs)
  1230. {
  1231. struct list_head *pos, *tmp;
  1232. int i;
  1233. mutex_lock(&pcs->mutex);
  1234. for (i = 0; i < pcs->ngroups; i++) {
  1235. struct pcs_pingroup *pingroup;
  1236. pingroup = radix_tree_lookup(&pcs->pgtree, i);
  1237. if (!pingroup)
  1238. continue;
  1239. radix_tree_delete(&pcs->pgtree, i);
  1240. }
  1241. list_for_each_safe(pos, tmp, &pcs->pingroups) {
  1242. struct pcs_pingroup *pingroup;
  1243. pingroup = list_entry(pos, struct pcs_pingroup, node);
  1244. list_del(&pingroup->node);
  1245. }
  1246. mutex_unlock(&pcs->mutex);
  1247. }
  1248. /**
  1249. * pcs_free_resources() - free memory used by this driver
  1250. * @pcs: pcs driver instance
  1251. */
  1252. static void pcs_free_resources(struct pcs_device *pcs)
  1253. {
  1254. if (pcs->pctl)
  1255. pinctrl_unregister(pcs->pctl);
  1256. pcs_free_funcs(pcs);
  1257. pcs_free_pingroups(pcs);
  1258. }
  1259. #define PCS_GET_PROP_U32(name, reg, err) \
  1260. do { \
  1261. ret = of_property_read_u32(np, name, reg); \
  1262. if (ret) { \
  1263. dev_err(pcs->dev, err); \
  1264. return ret; \
  1265. } \
  1266. } while (0);
  1267. static struct of_device_id pcs_of_match[];
  1268. static int pcs_add_gpio_func(struct device_node *node, struct pcs_device *pcs)
  1269. {
  1270. const char *propname = "pinctrl-single,gpio-range";
  1271. const char *cellname = "#pinctrl-single,gpio-range-cells";
  1272. struct of_phandle_args gpiospec;
  1273. struct pcs_gpiofunc_range *range;
  1274. int ret, i;
  1275. for (i = 0; ; i++) {
  1276. ret = of_parse_phandle_with_args(node, propname, cellname,
  1277. i, &gpiospec);
  1278. /* Do not treat it as error. Only treat it as end condition. */
  1279. if (ret) {
  1280. ret = 0;
  1281. break;
  1282. }
  1283. range = devm_kzalloc(pcs->dev, sizeof(*range), GFP_KERNEL);
  1284. if (!range) {
  1285. ret = -ENOMEM;
  1286. break;
  1287. }
  1288. range->offset = gpiospec.args[0];
  1289. range->npins = gpiospec.args[1];
  1290. range->gpiofunc = gpiospec.args[2];
  1291. mutex_lock(&pcs->mutex);
  1292. list_add_tail(&range->node, &pcs->gpiofuncs);
  1293. mutex_unlock(&pcs->mutex);
  1294. }
  1295. return ret;
  1296. }
  1297. #ifdef CONFIG_PM
  1298. static int pinctrl_single_suspend(struct platform_device *pdev,
  1299. pm_message_t state)
  1300. {
  1301. struct pcs_device *pcs;
  1302. pcs = platform_get_drvdata(pdev);
  1303. if (!pcs)
  1304. return -EINVAL;
  1305. return pinctrl_force_sleep(pcs->pctl);
  1306. }
  1307. static int pinctrl_single_resume(struct platform_device *pdev)
  1308. {
  1309. struct pcs_device *pcs;
  1310. pcs = platform_get_drvdata(pdev);
  1311. if (!pcs)
  1312. return -EINVAL;
  1313. return pinctrl_force_default(pcs->pctl);
  1314. }
  1315. #endif
  1316. static int pcs_probe(struct platform_device *pdev)
  1317. {
  1318. struct device_node *np = pdev->dev.of_node;
  1319. const struct of_device_id *match;
  1320. struct resource *res;
  1321. struct pcs_device *pcs;
  1322. int ret;
  1323. match = of_match_device(pcs_of_match, &pdev->dev);
  1324. if (!match)
  1325. return -EINVAL;
  1326. pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL);
  1327. if (!pcs) {
  1328. dev_err(&pdev->dev, "could not allocate\n");
  1329. return -ENOMEM;
  1330. }
  1331. pcs->dev = &pdev->dev;
  1332. mutex_init(&pcs->mutex);
  1333. INIT_LIST_HEAD(&pcs->pingroups);
  1334. INIT_LIST_HEAD(&pcs->functions);
  1335. INIT_LIST_HEAD(&pcs->gpiofuncs);
  1336. pcs->is_pinconf = match->data;
  1337. PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs->width,
  1338. "register width not specified\n");
  1339. ret = of_property_read_u32(np, "pinctrl-single,function-mask",
  1340. &pcs->fmask);
  1341. if (!ret) {
  1342. pcs->fshift = ffs(pcs->fmask) - 1;
  1343. pcs->fmax = pcs->fmask >> pcs->fshift;
  1344. } else {
  1345. /* If mask property doesn't exist, function mux is invalid. */
  1346. pcs->fmask = 0;
  1347. pcs->fshift = 0;
  1348. pcs->fmax = 0;
  1349. }
  1350. ret = of_property_read_u32(np, "pinctrl-single,function-off",
  1351. &pcs->foff);
  1352. if (ret)
  1353. pcs->foff = PCS_OFF_DISABLED;
  1354. pcs->bits_per_mux = of_property_read_bool(np,
  1355. "pinctrl-single,bit-per-mux");
  1356. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1357. if (!res) {
  1358. dev_err(pcs->dev, "could not get resource\n");
  1359. return -ENODEV;
  1360. }
  1361. pcs->res = devm_request_mem_region(pcs->dev, res->start,
  1362. resource_size(res), DRIVER_NAME);
  1363. if (!pcs->res) {
  1364. dev_err(pcs->dev, "could not get mem_region\n");
  1365. return -EBUSY;
  1366. }
  1367. pcs->size = resource_size(pcs->res);
  1368. pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size);
  1369. if (!pcs->base) {
  1370. dev_err(pcs->dev, "could not ioremap\n");
  1371. return -ENODEV;
  1372. }
  1373. INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL);
  1374. INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL);
  1375. platform_set_drvdata(pdev, pcs);
  1376. switch (pcs->width) {
  1377. case 8:
  1378. pcs->read = pcs_readb;
  1379. pcs->write = pcs_writeb;
  1380. break;
  1381. case 16:
  1382. pcs->read = pcs_readw;
  1383. pcs->write = pcs_writew;
  1384. break;
  1385. case 32:
  1386. pcs->read = pcs_readl;
  1387. pcs->write = pcs_writel;
  1388. break;
  1389. default:
  1390. break;
  1391. }
  1392. pcs->desc.name = DRIVER_NAME;
  1393. pcs->desc.pctlops = &pcs_pinctrl_ops;
  1394. pcs->desc.pmxops = &pcs_pinmux_ops;
  1395. if (pcs->is_pinconf)
  1396. pcs->desc.confops = &pcs_pinconf_ops;
  1397. pcs->desc.owner = THIS_MODULE;
  1398. ret = pcs_allocate_pin_table(pcs);
  1399. if (ret < 0)
  1400. goto free;
  1401. pcs->pctl = pinctrl_register(&pcs->desc, pcs->dev, pcs);
  1402. if (!pcs->pctl) {
  1403. dev_err(pcs->dev, "could not register single pinctrl driver\n");
  1404. ret = -EINVAL;
  1405. goto free;
  1406. }
  1407. ret = pcs_add_gpio_func(np, pcs);
  1408. if (ret < 0)
  1409. goto free;
  1410. dev_info(pcs->dev, "%i pins at pa %p size %u\n",
  1411. pcs->desc.npins, pcs->base, pcs->size);
  1412. return 0;
  1413. free:
  1414. pcs_free_resources(pcs);
  1415. return ret;
  1416. }
  1417. static int pcs_remove(struct platform_device *pdev)
  1418. {
  1419. struct pcs_device *pcs = platform_get_drvdata(pdev);
  1420. if (!pcs)
  1421. return 0;
  1422. pcs_free_resources(pcs);
  1423. return 0;
  1424. }
  1425. static struct of_device_id pcs_of_match[] = {
  1426. { .compatible = "pinctrl-single", .data = (void *)false },
  1427. { .compatible = "pinconf-single", .data = (void *)true },
  1428. { },
  1429. };
  1430. MODULE_DEVICE_TABLE(of, pcs_of_match);
  1431. static struct platform_driver pcs_driver = {
  1432. .probe = pcs_probe,
  1433. .remove = pcs_remove,
  1434. .driver = {
  1435. .owner = THIS_MODULE,
  1436. .name = DRIVER_NAME,
  1437. .of_match_table = pcs_of_match,
  1438. },
  1439. #ifdef CONFIG_PM
  1440. .suspend = pinctrl_single_suspend,
  1441. .resume = pinctrl_single_resume,
  1442. #endif
  1443. };
  1444. module_platform_driver(pcs_driver);
  1445. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  1446. MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver");
  1447. MODULE_LICENSE("GPL v2");