pinctrl-single.c 46 KB

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