pinctrl-single.c 41 KB

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