pinctrl-single.c 45 KB

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