pinctrl-tz1090-pdc.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * Pinctrl driver for the Toumaz Xenif TZ1090 PowerDown Controller pins
  3. *
  4. * Copyright (c) 2013, Imagination Technologies Ltd.
  5. *
  6. * Derived from Tegra code:
  7. * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved.
  8. *
  9. * Derived from code:
  10. * Copyright (C) 2010 Google, Inc.
  11. * Copyright (C) 2010 NVIDIA Corporation
  12. * Copyright (C) 2009-2011 ST-Ericsson AB
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms and conditions of the GNU General Public License,
  16. * version 2, as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope it will be useful, but WITHOUT
  19. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  20. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  21. * more details.
  22. */
  23. #include <linux/bitops.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pinctrl/machine.h>
  29. #include <linux/pinctrl/pinconf-generic.h>
  30. #include <linux/pinctrl/pinctrl.h>
  31. #include <linux/pinctrl/pinmux.h>
  32. #include <linux/slab.h>
  33. /*
  34. * The registers may be shared with other threads/cores, so we need to use the
  35. * metag global lock2 for atomicity.
  36. */
  37. #include <asm/global_lock.h>
  38. #include "core.h"
  39. #include "pinconf.h"
  40. /* Register offsets from bank base address */
  41. #define REG_GPIO_CONTROL0 0x00
  42. #define REG_GPIO_CONTROL2 0x08
  43. /* Register field information */
  44. #define REG_GPIO_CONTROL2_PU_PD_S 16
  45. #define REG_GPIO_CONTROL2_PDC_POS_S 4
  46. #define REG_GPIO_CONTROL2_PDC_DR_S 2
  47. #define REG_GPIO_CONTROL2_PDC_SR_S 1
  48. #define REG_GPIO_CONTROL2_PDC_SCHMITT_S 0
  49. /* PU_PD field values */
  50. #define REG_PU_PD_TRISTATE 0
  51. #define REG_PU_PD_UP 1
  52. #define REG_PU_PD_DOWN 2
  53. #define REG_PU_PD_REPEATER 3
  54. /* DR field values */
  55. #define REG_DR_2mA 0
  56. #define REG_DR_4mA 1
  57. #define REG_DR_8mA 2
  58. #define REG_DR_12mA 3
  59. /**
  60. * struct tz1090_pdc_function - TZ1090 PDC pinctrl mux function
  61. * @name: The name of the function, exported to pinctrl core.
  62. * @groups: An array of pin groups that may select this function.
  63. * @ngroups: The number of entries in @groups.
  64. */
  65. struct tz1090_pdc_function {
  66. const char *name;
  67. const char * const *groups;
  68. unsigned int ngroups;
  69. };
  70. /**
  71. * struct tz1090_pdc_pingroup - TZ1090 PDC pin group
  72. * @name: Name of pin group.
  73. * @pins: Array of pin numbers in this pin group.
  74. * @npins: Number of pins in this pin group.
  75. * @func: Function enabled by the mux.
  76. * @reg: Mux register offset.
  77. * @bit: Mux register bit.
  78. * @drv: Drive control supported, otherwise it's a mux.
  79. * This means Schmitt, Slew, and Drive strength.
  80. *
  81. * A representation of a group of pins (possibly just one pin) in the TZ1090
  82. * PDC pin controller. Each group allows some parameter or parameters to be
  83. * configured. The most common is mux function selection.
  84. */
  85. struct tz1090_pdc_pingroup {
  86. const char *name;
  87. const unsigned int *pins;
  88. unsigned int npins;
  89. int func;
  90. u16 reg;
  91. u8 bit;
  92. bool drv;
  93. };
  94. /*
  95. * All PDC pins can be GPIOs. Define these first to match how the GPIO driver
  96. * names/numbers its pins.
  97. */
  98. enum tz1090_pdc_pin {
  99. TZ1090_PDC_PIN_GPIO0,
  100. TZ1090_PDC_PIN_GPIO1,
  101. TZ1090_PDC_PIN_SYS_WAKE0,
  102. TZ1090_PDC_PIN_SYS_WAKE1,
  103. TZ1090_PDC_PIN_SYS_WAKE2,
  104. TZ1090_PDC_PIN_IR_DATA,
  105. TZ1090_PDC_PIN_EXT_POWER,
  106. };
  107. /* Pin names */
  108. static const struct pinctrl_pin_desc tz1090_pdc_pins[] = {
  109. /* PDC GPIOs */
  110. PINCTRL_PIN(TZ1090_PDC_PIN_GPIO0, "gpio0"),
  111. PINCTRL_PIN(TZ1090_PDC_PIN_GPIO1, "gpio1"),
  112. PINCTRL_PIN(TZ1090_PDC_PIN_SYS_WAKE0, "sys_wake0"),
  113. PINCTRL_PIN(TZ1090_PDC_PIN_SYS_WAKE1, "sys_wake1"),
  114. PINCTRL_PIN(TZ1090_PDC_PIN_SYS_WAKE2, "sys_wake2"),
  115. PINCTRL_PIN(TZ1090_PDC_PIN_IR_DATA, "ir_data"),
  116. PINCTRL_PIN(TZ1090_PDC_PIN_EXT_POWER, "ext_power"),
  117. };
  118. /* Pin group pins */
  119. static const unsigned int gpio0_pins[] = {
  120. TZ1090_PDC_PIN_GPIO0,
  121. };
  122. static const unsigned int gpio1_pins[] = {
  123. TZ1090_PDC_PIN_GPIO1,
  124. };
  125. static const unsigned int pdc_pins[] = {
  126. TZ1090_PDC_PIN_GPIO0,
  127. TZ1090_PDC_PIN_GPIO1,
  128. TZ1090_PDC_PIN_SYS_WAKE0,
  129. TZ1090_PDC_PIN_SYS_WAKE1,
  130. TZ1090_PDC_PIN_SYS_WAKE2,
  131. TZ1090_PDC_PIN_IR_DATA,
  132. TZ1090_PDC_PIN_EXT_POWER,
  133. };
  134. /* Mux functions */
  135. enum tz1090_pdc_mux {
  136. /* PDC_GPIO0 mux */
  137. TZ1090_PDC_MUX_IR_MOD_STABLE_OUT,
  138. /* PDC_GPIO1 mux */
  139. TZ1090_PDC_MUX_IR_MOD_POWER_OUT,
  140. };
  141. /* Pin groups a function can be muxed to */
  142. static const char * const gpio0_groups[] = {
  143. "gpio0",
  144. };
  145. static const char * const gpio1_groups[] = {
  146. "gpio1",
  147. };
  148. #define FUNCTION(mux, fname, group) \
  149. [(TZ1090_PDC_MUX_ ## mux)] = { \
  150. .name = #fname, \
  151. .groups = group##_groups, \
  152. .ngroups = ARRAY_SIZE(group##_groups), \
  153. }
  154. /* Must correlate with enum tz1090_pdc_mux */
  155. static const struct tz1090_pdc_function tz1090_pdc_functions[] = {
  156. /* MUX fn pingroups */
  157. FUNCTION(IR_MOD_STABLE_OUT, ir_mod_stable_out, gpio0),
  158. FUNCTION(IR_MOD_POWER_OUT, ir_mod_power_out, gpio1),
  159. };
  160. /**
  161. * MUX_PG() - Initialise a pin group with mux control
  162. * @pg_name: Pin group name (stringified, _pins appended to get pins array)
  163. * @f0: Function 0 (TZ1090_PDC_MUX_ is prepended)
  164. * @mux_r: Mux register (REG_PINCTRL_ is prepended)
  165. * @mux_b: Bit number in register of mux field
  166. */
  167. #define MUX_PG(pg_name, f0, mux_r, mux_b) \
  168. { \
  169. .name = #pg_name, \
  170. .pins = pg_name##_pins, \
  171. .npins = ARRAY_SIZE(pg_name##_pins), \
  172. .func = TZ1090_PDC_MUX_ ## f0, \
  173. .reg = (REG_ ## mux_r), \
  174. .bit = (mux_b), \
  175. }
  176. /**
  177. * DRV_PG() - Initialise a pin group with drive control
  178. * @pg_name: Pin group name (stringified, _pins appended to get pins array)
  179. */
  180. #define DRV_PG(pg_name) \
  181. { \
  182. .name = #pg_name, \
  183. .pins = pg_name##_pins, \
  184. .npins = ARRAY_SIZE(pg_name##_pins), \
  185. .drv = true, \
  186. }
  187. static const struct tz1090_pdc_pingroup tz1090_pdc_groups[] = {
  188. /* Muxing pin groups */
  189. /* pg_name, f0, mux register, mux bit */
  190. MUX_PG(gpio0, IR_MOD_STABLE_OUT, GPIO_CONTROL0, 7),
  191. MUX_PG(gpio1, IR_MOD_POWER_OUT, GPIO_CONTROL0, 6),
  192. /* Drive pin groups */
  193. /* pg_name */
  194. DRV_PG(pdc),
  195. };
  196. /**
  197. * struct tz1090_pdc_pmx - Private pinctrl data
  198. * @dev: Platform device
  199. * @pctl: Pin control device
  200. * @regs: Register region
  201. * @lock: Lock protecting coherency of mux_en and gpio_en
  202. * @mux_en: Muxes that have been enabled
  203. * @gpio_en: Muxable GPIOs that have been enabled
  204. */
  205. struct tz1090_pdc_pmx {
  206. struct device *dev;
  207. struct pinctrl_dev *pctl;
  208. void __iomem *regs;
  209. spinlock_t lock;
  210. u32 mux_en;
  211. u32 gpio_en;
  212. };
  213. static inline u32 pmx_read(struct tz1090_pdc_pmx *pmx, u32 reg)
  214. {
  215. return ioread32(pmx->regs + reg);
  216. }
  217. static inline void pmx_write(struct tz1090_pdc_pmx *pmx, u32 val, u32 reg)
  218. {
  219. iowrite32(val, pmx->regs + reg);
  220. }
  221. /*
  222. * Pin control operations
  223. */
  224. static int tz1090_pdc_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  225. {
  226. return ARRAY_SIZE(tz1090_pdc_groups);
  227. }
  228. static const char *tz1090_pdc_pinctrl_get_group_name(struct pinctrl_dev *pctl,
  229. unsigned int group)
  230. {
  231. return tz1090_pdc_groups[group].name;
  232. }
  233. static int tz1090_pdc_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  234. unsigned int group,
  235. const unsigned int **pins,
  236. unsigned int *num_pins)
  237. {
  238. *pins = tz1090_pdc_groups[group].pins;
  239. *num_pins = tz1090_pdc_groups[group].npins;
  240. return 0;
  241. }
  242. #ifdef CONFIG_DEBUG_FS
  243. static void tz1090_pdc_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
  244. struct seq_file *s,
  245. unsigned int offset)
  246. {
  247. seq_printf(s, " %s", dev_name(pctldev->dev));
  248. }
  249. #endif
  250. static int reserve_map(struct device *dev, struct pinctrl_map **map,
  251. unsigned int *reserved_maps, unsigned int *num_maps,
  252. unsigned int reserve)
  253. {
  254. unsigned int old_num = *reserved_maps;
  255. unsigned int new_num = *num_maps + reserve;
  256. struct pinctrl_map *new_map;
  257. if (old_num >= new_num)
  258. return 0;
  259. new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
  260. if (!new_map) {
  261. dev_err(dev, "krealloc(map) failed\n");
  262. return -ENOMEM;
  263. }
  264. memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
  265. *map = new_map;
  266. *reserved_maps = new_num;
  267. return 0;
  268. }
  269. static int add_map_mux(struct pinctrl_map **map, unsigned int *reserved_maps,
  270. unsigned int *num_maps, const char *group,
  271. const char *function)
  272. {
  273. if (WARN_ON(*num_maps == *reserved_maps))
  274. return -ENOSPC;
  275. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  276. (*map)[*num_maps].data.mux.group = group;
  277. (*map)[*num_maps].data.mux.function = function;
  278. (*num_maps)++;
  279. return 0;
  280. }
  281. /**
  282. * get_group_selector() - returns the group selector for a group
  283. * @pin_group: the pin group to look up
  284. *
  285. * This is the same as pinctrl_get_group_selector except it doesn't produce an
  286. * error message if the group isn't found or debug messages.
  287. */
  288. static int get_group_selector(const char *pin_group)
  289. {
  290. unsigned int group;
  291. for (group = 0; group < ARRAY_SIZE(tz1090_pdc_groups); ++group)
  292. if (!strcmp(tz1090_pdc_groups[group].name, pin_group))
  293. return group;
  294. return -EINVAL;
  295. }
  296. static int add_map_configs(struct device *dev,
  297. struct pinctrl_map **map,
  298. unsigned int *reserved_maps, unsigned int *num_maps,
  299. const char *group, unsigned long *configs,
  300. unsigned int num_configs)
  301. {
  302. unsigned long *dup_configs;
  303. enum pinctrl_map_type type;
  304. if (WARN_ON(*num_maps == *reserved_maps))
  305. return -ENOSPC;
  306. dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
  307. GFP_KERNEL);
  308. if (!dup_configs) {
  309. dev_err(dev, "kmemdup(configs) failed\n");
  310. return -ENOMEM;
  311. }
  312. /*
  313. * We support both pins and pin groups, but we need to figure out which
  314. * one we have.
  315. */
  316. if (get_group_selector(group) >= 0)
  317. type = PIN_MAP_TYPE_CONFIGS_GROUP;
  318. else
  319. type = PIN_MAP_TYPE_CONFIGS_PIN;
  320. (*map)[*num_maps].type = type;
  321. (*map)[*num_maps].data.configs.group_or_pin = group;
  322. (*map)[*num_maps].data.configs.configs = dup_configs;
  323. (*map)[*num_maps].data.configs.num_configs = num_configs;
  324. (*num_maps)++;
  325. return 0;
  326. }
  327. static void tz1090_pdc_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
  328. struct pinctrl_map *map,
  329. unsigned int num_maps)
  330. {
  331. int i;
  332. for (i = 0; i < num_maps; i++)
  333. if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
  334. kfree(map[i].data.configs.configs);
  335. kfree(map);
  336. }
  337. static int tz1090_pdc_pinctrl_dt_subnode_to_map(struct device *dev,
  338. struct device_node *np,
  339. struct pinctrl_map **map,
  340. unsigned int *reserved_maps,
  341. unsigned int *num_maps)
  342. {
  343. int ret;
  344. const char *function;
  345. unsigned long *configs = NULL;
  346. unsigned int num_configs = 0;
  347. unsigned int reserve;
  348. struct property *prop;
  349. const char *group;
  350. ret = of_property_read_string(np, "tz1090,function", &function);
  351. if (ret < 0) {
  352. /* EINVAL=missing, which is fine since it's optional */
  353. if (ret != -EINVAL)
  354. dev_err(dev,
  355. "could not parse property function\n");
  356. function = NULL;
  357. }
  358. ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
  359. if (ret)
  360. return ret;
  361. reserve = 0;
  362. if (function != NULL)
  363. reserve++;
  364. if (num_configs)
  365. reserve++;
  366. ret = of_property_count_strings(np, "tz1090,pins");
  367. if (ret < 0) {
  368. dev_err(dev, "could not parse property pins\n");
  369. goto exit;
  370. }
  371. reserve *= ret;
  372. ret = reserve_map(dev, map, reserved_maps, num_maps, reserve);
  373. if (ret < 0)
  374. goto exit;
  375. of_property_for_each_string(np, "tz1090,pins", prop, group) {
  376. if (function) {
  377. ret = add_map_mux(map, reserved_maps, num_maps,
  378. group, function);
  379. if (ret < 0)
  380. goto exit;
  381. }
  382. if (num_configs) {
  383. ret = add_map_configs(dev, map, reserved_maps,
  384. num_maps, group, configs,
  385. num_configs);
  386. if (ret < 0)
  387. goto exit;
  388. }
  389. }
  390. ret = 0;
  391. exit:
  392. kfree(configs);
  393. return ret;
  394. }
  395. static int tz1090_pdc_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  396. struct device_node *np_config,
  397. struct pinctrl_map **map,
  398. unsigned int *num_maps)
  399. {
  400. unsigned int reserved_maps;
  401. struct device_node *np;
  402. int ret;
  403. reserved_maps = 0;
  404. *map = NULL;
  405. *num_maps = 0;
  406. for_each_child_of_node(np_config, np) {
  407. ret = tz1090_pdc_pinctrl_dt_subnode_to_map(pctldev->dev, np,
  408. map, &reserved_maps,
  409. num_maps);
  410. if (ret < 0) {
  411. tz1090_pdc_pinctrl_dt_free_map(pctldev, *map,
  412. *num_maps);
  413. return ret;
  414. }
  415. }
  416. return 0;
  417. }
  418. static struct pinctrl_ops tz1090_pdc_pinctrl_ops = {
  419. .get_groups_count = tz1090_pdc_pinctrl_get_groups_count,
  420. .get_group_name = tz1090_pdc_pinctrl_get_group_name,
  421. .get_group_pins = tz1090_pdc_pinctrl_get_group_pins,
  422. #ifdef CONFIG_DEBUG_FS
  423. .pin_dbg_show = tz1090_pdc_pinctrl_pin_dbg_show,
  424. #endif
  425. .dt_node_to_map = tz1090_pdc_pinctrl_dt_node_to_map,
  426. .dt_free_map = tz1090_pdc_pinctrl_dt_free_map,
  427. };
  428. /*
  429. * Pin mux operations
  430. */
  431. static int tz1090_pdc_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
  432. {
  433. return ARRAY_SIZE(tz1090_pdc_functions);
  434. }
  435. static const char *tz1090_pdc_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
  436. unsigned int function)
  437. {
  438. return tz1090_pdc_functions[function].name;
  439. }
  440. static int tz1090_pdc_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
  441. unsigned int function,
  442. const char * const **groups,
  443. unsigned int * const num_groups)
  444. {
  445. *groups = tz1090_pdc_functions[function].groups;
  446. *num_groups = tz1090_pdc_functions[function].ngroups;
  447. return 0;
  448. }
  449. /**
  450. * tz1090_pdc_pinctrl_mux() - update mux bit
  451. * @pmx: Pinmux data
  452. * @grp: Pin mux group
  453. */
  454. static void tz1090_pdc_pinctrl_mux(struct tz1090_pdc_pmx *pmx,
  455. const struct tz1090_pdc_pingroup *grp)
  456. {
  457. u32 reg, select;
  458. unsigned int pin_shift = grp->pins[0];
  459. unsigned long flags;
  460. /* select = mux && !gpio */
  461. select = ((pmx->mux_en & ~pmx->gpio_en) >> pin_shift) & 1;
  462. /* set up the mux */
  463. __global_lock2(flags);
  464. reg = pmx_read(pmx, grp->reg);
  465. reg &= ~BIT(grp->bit);
  466. reg |= select << grp->bit;
  467. pmx_write(pmx, reg, grp->reg);
  468. __global_unlock2(flags);
  469. }
  470. static int tz1090_pdc_pinctrl_set_mux(struct pinctrl_dev *pctldev,
  471. unsigned int function,
  472. unsigned int group)
  473. {
  474. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  475. const struct tz1090_pdc_pingroup *grp = &tz1090_pdc_groups[group];
  476. dev_dbg(pctldev->dev, "%s(func=%u (%s), group=%u (%s))\n",
  477. __func__,
  478. function, tz1090_pdc_functions[function].name,
  479. group, tz1090_pdc_groups[group].name);
  480. /* is it even a mux? */
  481. if (grp->drv)
  482. return -EINVAL;
  483. /* does this group even control the function? */
  484. if (function != grp->func)
  485. return -EINVAL;
  486. /* record the pin being muxed and update mux bit */
  487. spin_lock(&pmx->lock);
  488. pmx->mux_en |= BIT(grp->pins[0]);
  489. tz1090_pdc_pinctrl_mux(pmx, grp);
  490. spin_unlock(&pmx->lock);
  491. return 0;
  492. }
  493. static const struct tz1090_pdc_pingroup *find_mux_group(
  494. struct tz1090_pdc_pmx *pmx,
  495. unsigned int pin)
  496. {
  497. const struct tz1090_pdc_pingroup *grp;
  498. unsigned int group;
  499. grp = tz1090_pdc_groups;
  500. for (group = 0; group < ARRAY_SIZE(tz1090_pdc_groups); ++group, ++grp) {
  501. /* only match muxes */
  502. if (grp->drv)
  503. continue;
  504. /* with a matching pin */
  505. if (grp->pins[0] == pin)
  506. return grp;
  507. }
  508. return NULL;
  509. }
  510. static int tz1090_pdc_pinctrl_gpio_request_enable(
  511. struct pinctrl_dev *pctldev,
  512. struct pinctrl_gpio_range *range,
  513. unsigned int pin)
  514. {
  515. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  516. const struct tz1090_pdc_pingroup *grp = find_mux_group(pmx, pin);
  517. if (grp) {
  518. /* record the pin in GPIO use and update mux bit */
  519. spin_lock(&pmx->lock);
  520. pmx->gpio_en |= BIT(pin);
  521. tz1090_pdc_pinctrl_mux(pmx, grp);
  522. spin_unlock(&pmx->lock);
  523. }
  524. return 0;
  525. }
  526. static void tz1090_pdc_pinctrl_gpio_disable_free(
  527. struct pinctrl_dev *pctldev,
  528. struct pinctrl_gpio_range *range,
  529. unsigned int pin)
  530. {
  531. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  532. const struct tz1090_pdc_pingroup *grp = find_mux_group(pmx, pin);
  533. if (grp) {
  534. /* record the pin not in GPIO use and update mux bit */
  535. spin_lock(&pmx->lock);
  536. pmx->gpio_en &= ~BIT(pin);
  537. tz1090_pdc_pinctrl_mux(pmx, grp);
  538. spin_unlock(&pmx->lock);
  539. }
  540. }
  541. static struct pinmux_ops tz1090_pdc_pinmux_ops = {
  542. .get_functions_count = tz1090_pdc_pinctrl_get_funcs_count,
  543. .get_function_name = tz1090_pdc_pinctrl_get_func_name,
  544. .get_function_groups = tz1090_pdc_pinctrl_get_func_groups,
  545. .set_mux = tz1090_pdc_pinctrl_set_mux,
  546. .gpio_request_enable = tz1090_pdc_pinctrl_gpio_request_enable,
  547. .gpio_disable_free = tz1090_pdc_pinctrl_gpio_disable_free,
  548. };
  549. /*
  550. * Pin config operations
  551. */
  552. static int tz1090_pdc_pinconf_reg(struct pinctrl_dev *pctldev,
  553. unsigned int pin,
  554. enum pin_config_param param,
  555. bool report_err,
  556. u32 *reg, u32 *width, u32 *mask, u32 *shift,
  557. u32 *val)
  558. {
  559. /* Find information about parameter's register */
  560. switch (param) {
  561. case PIN_CONFIG_BIAS_DISABLE:
  562. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  563. *val = REG_PU_PD_TRISTATE;
  564. break;
  565. case PIN_CONFIG_BIAS_PULL_UP:
  566. *val = REG_PU_PD_UP;
  567. break;
  568. case PIN_CONFIG_BIAS_PULL_DOWN:
  569. *val = REG_PU_PD_DOWN;
  570. break;
  571. case PIN_CONFIG_BIAS_BUS_HOLD:
  572. *val = REG_PU_PD_REPEATER;
  573. break;
  574. default:
  575. return -ENOTSUPP;
  576. }
  577. /* Only input bias parameters supported */
  578. *reg = REG_GPIO_CONTROL2;
  579. *shift = REG_GPIO_CONTROL2_PU_PD_S + pin*2;
  580. *width = 2;
  581. /* Calculate field information */
  582. *mask = (BIT(*width) - 1) << *shift;
  583. return 0;
  584. }
  585. static int tz1090_pdc_pinconf_get(struct pinctrl_dev *pctldev,
  586. unsigned int pin, unsigned long *config)
  587. {
  588. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  589. enum pin_config_param param = pinconf_to_config_param(*config);
  590. int ret;
  591. u32 reg, width, mask, shift, val, tmp, arg;
  592. /* Get register information */
  593. ret = tz1090_pdc_pinconf_reg(pctldev, pin, param, true,
  594. &reg, &width, &mask, &shift, &val);
  595. if (ret < 0)
  596. return ret;
  597. /* Extract field from register */
  598. tmp = pmx_read(pmx, reg);
  599. arg = ((tmp & mask) >> shift) == val;
  600. /* Config not active */
  601. if (!arg)
  602. return -EINVAL;
  603. /* And pack config */
  604. *config = pinconf_to_config_packed(param, arg);
  605. return 0;
  606. }
  607. static int tz1090_pdc_pinconf_set(struct pinctrl_dev *pctldev,
  608. unsigned int pin, unsigned long *configs,
  609. unsigned num_configs)
  610. {
  611. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  612. enum pin_config_param param;
  613. unsigned int arg;
  614. int ret;
  615. u32 reg, width, mask, shift, val, tmp;
  616. unsigned long flags;
  617. int i;
  618. for (i = 0; i < num_configs; i++) {
  619. param = pinconf_to_config_param(configs[i]);
  620. arg = pinconf_to_config_argument(configs[i]);
  621. dev_dbg(pctldev->dev, "%s(pin=%s, config=%#lx)\n",
  622. __func__, tz1090_pdc_pins[pin].name, configs[i]);
  623. /* Get register information */
  624. ret = tz1090_pdc_pinconf_reg(pctldev, pin, param, true,
  625. &reg, &width, &mask, &shift, &val);
  626. if (ret < 0)
  627. return ret;
  628. /* Unpack argument and range check it */
  629. if (arg > 1) {
  630. dev_dbg(pctldev->dev, "%s: arg %u out of range\n",
  631. __func__, arg);
  632. return -EINVAL;
  633. }
  634. /* Write register field */
  635. __global_lock2(flags);
  636. tmp = pmx_read(pmx, reg);
  637. tmp &= ~mask;
  638. if (arg)
  639. tmp |= val << shift;
  640. pmx_write(pmx, tmp, reg);
  641. __global_unlock2(flags);
  642. } /* for each config */
  643. return 0;
  644. }
  645. static const int tz1090_pdc_boolean_map[] = {
  646. [0] = -EINVAL,
  647. [1] = 1,
  648. };
  649. static const int tz1090_pdc_dr_map[] = {
  650. [REG_DR_2mA] = 2,
  651. [REG_DR_4mA] = 4,
  652. [REG_DR_8mA] = 8,
  653. [REG_DR_12mA] = 12,
  654. };
  655. static int tz1090_pdc_pinconf_group_reg(struct pinctrl_dev *pctldev,
  656. const struct tz1090_pdc_pingroup *g,
  657. enum pin_config_param param,
  658. bool report_err, u32 *reg, u32 *width,
  659. u32 *mask, u32 *shift, const int **map)
  660. {
  661. /* Drive configuration applies in groups, but not to all groups. */
  662. if (!g->drv) {
  663. if (report_err)
  664. dev_dbg(pctldev->dev,
  665. "%s: group %s has no drive control\n",
  666. __func__, g->name);
  667. return -ENOTSUPP;
  668. }
  669. /* Find information about drive parameter's register */
  670. *reg = REG_GPIO_CONTROL2;
  671. switch (param) {
  672. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  673. *shift = REG_GPIO_CONTROL2_PDC_SCHMITT_S;
  674. *width = 1;
  675. *map = tz1090_pdc_boolean_map;
  676. break;
  677. case PIN_CONFIG_DRIVE_STRENGTH:
  678. *shift = REG_GPIO_CONTROL2_PDC_DR_S;
  679. *width = 2;
  680. *map = tz1090_pdc_dr_map;
  681. break;
  682. case PIN_CONFIG_LOW_POWER_MODE:
  683. *shift = REG_GPIO_CONTROL2_PDC_POS_S;
  684. *width = 1;
  685. *map = tz1090_pdc_boolean_map;
  686. break;
  687. default:
  688. return -ENOTSUPP;
  689. }
  690. /* Calculate field information */
  691. *mask = (BIT(*width) - 1) << *shift;
  692. return 0;
  693. }
  694. static int tz1090_pdc_pinconf_group_get(struct pinctrl_dev *pctldev,
  695. unsigned int group,
  696. unsigned long *config)
  697. {
  698. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  699. const struct tz1090_pdc_pingroup *g = &tz1090_pdc_groups[group];
  700. enum pin_config_param param = pinconf_to_config_param(*config);
  701. int ret, arg;
  702. u32 reg, width, mask, shift, val;
  703. const int *map;
  704. /* Get register information */
  705. ret = tz1090_pdc_pinconf_group_reg(pctldev, g, param, true,
  706. &reg, &width, &mask, &shift, &map);
  707. if (ret < 0)
  708. return ret;
  709. /* Extract field from register */
  710. val = pmx_read(pmx, reg);
  711. arg = map[(val & mask) >> shift];
  712. if (arg < 0)
  713. return arg;
  714. /* And pack config */
  715. *config = pinconf_to_config_packed(param, arg);
  716. return 0;
  717. }
  718. static int tz1090_pdc_pinconf_group_set(struct pinctrl_dev *pctldev,
  719. unsigned int group,
  720. unsigned long *configs,
  721. unsigned num_configs)
  722. {
  723. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  724. const struct tz1090_pdc_pingroup *g = &tz1090_pdc_groups[group];
  725. enum pin_config_param param;
  726. const unsigned int *pit;
  727. unsigned int i;
  728. int ret, arg;
  729. u32 reg, width, mask, shift, val;
  730. unsigned long flags;
  731. const int *map;
  732. int j;
  733. for (j = 0; j < num_configs; j++) {
  734. param = pinconf_to_config_param(configs[j]);
  735. dev_dbg(pctldev->dev, "%s(group=%s, config=%#lx)\n",
  736. __func__, g->name, configs[j]);
  737. /* Get register information */
  738. ret = tz1090_pdc_pinconf_group_reg(pctldev, g, param, true,
  739. &reg, &width, &mask, &shift,
  740. &map);
  741. if (ret < 0) {
  742. /*
  743. * Maybe we're trying to set a per-pin configuration
  744. * of a group, so do the pins one by one. This is
  745. * mainly as a convenience.
  746. */
  747. for (i = 0, pit = g->pins; i < g->npins; ++i, ++pit) {
  748. ret = tz1090_pdc_pinconf_set(pctldev, *pit,
  749. configs, num_configs);
  750. if (ret)
  751. return ret;
  752. }
  753. return 0;
  754. }
  755. /* Unpack argument and map it to register value */
  756. arg = pinconf_to_config_argument(configs[j]);
  757. for (i = 0; i < BIT(width); ++i) {
  758. if (map[i] == arg || (map[i] == -EINVAL && !arg)) {
  759. /* Write register field */
  760. __global_lock2(flags);
  761. val = pmx_read(pmx, reg);
  762. val &= ~mask;
  763. val |= i << shift;
  764. pmx_write(pmx, val, reg);
  765. __global_unlock2(flags);
  766. goto next_config;
  767. }
  768. }
  769. dev_dbg(pctldev->dev, "%s: arg %u not supported\n",
  770. __func__, arg);
  771. return 0;
  772. next_config:
  773. ;
  774. } /* for each config */
  775. return 0;
  776. }
  777. static struct pinconf_ops tz1090_pdc_pinconf_ops = {
  778. .is_generic = true,
  779. .pin_config_get = tz1090_pdc_pinconf_get,
  780. .pin_config_set = tz1090_pdc_pinconf_set,
  781. .pin_config_group_get = tz1090_pdc_pinconf_group_get,
  782. .pin_config_group_set = tz1090_pdc_pinconf_group_set,
  783. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  784. };
  785. /*
  786. * Pin control driver setup
  787. */
  788. static struct pinctrl_desc tz1090_pdc_pinctrl_desc = {
  789. .pctlops = &tz1090_pdc_pinctrl_ops,
  790. .pmxops = &tz1090_pdc_pinmux_ops,
  791. .confops = &tz1090_pdc_pinconf_ops,
  792. .owner = THIS_MODULE,
  793. };
  794. static int tz1090_pdc_pinctrl_probe(struct platform_device *pdev)
  795. {
  796. struct tz1090_pdc_pmx *pmx;
  797. struct resource *res;
  798. pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
  799. if (!pmx) {
  800. dev_err(&pdev->dev, "Can't alloc tz1090_pdc_pmx\n");
  801. return -ENOMEM;
  802. }
  803. pmx->dev = &pdev->dev;
  804. spin_lock_init(&pmx->lock);
  805. tz1090_pdc_pinctrl_desc.name = dev_name(&pdev->dev);
  806. tz1090_pdc_pinctrl_desc.pins = tz1090_pdc_pins;
  807. tz1090_pdc_pinctrl_desc.npins = ARRAY_SIZE(tz1090_pdc_pins);
  808. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  809. pmx->regs = devm_ioremap_resource(&pdev->dev, res);
  810. if (IS_ERR(pmx->regs))
  811. return PTR_ERR(pmx->regs);
  812. pmx->pctl = pinctrl_register(&tz1090_pdc_pinctrl_desc, &pdev->dev, pmx);
  813. if (IS_ERR(pmx->pctl)) {
  814. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  815. return PTR_ERR(pmx->pctl);
  816. }
  817. platform_set_drvdata(pdev, pmx);
  818. dev_info(&pdev->dev, "TZ1090 PDC pinctrl driver initialised\n");
  819. return 0;
  820. }
  821. static int tz1090_pdc_pinctrl_remove(struct platform_device *pdev)
  822. {
  823. struct tz1090_pdc_pmx *pmx = platform_get_drvdata(pdev);
  824. pinctrl_unregister(pmx->pctl);
  825. return 0;
  826. }
  827. static const struct of_device_id tz1090_pdc_pinctrl_of_match[] = {
  828. { .compatible = "img,tz1090-pdc-pinctrl", },
  829. { },
  830. };
  831. static struct platform_driver tz1090_pdc_pinctrl_driver = {
  832. .driver = {
  833. .name = "tz1090-pdc-pinctrl",
  834. .of_match_table = tz1090_pdc_pinctrl_of_match,
  835. },
  836. .probe = tz1090_pdc_pinctrl_probe,
  837. .remove = tz1090_pdc_pinctrl_remove,
  838. };
  839. static int __init tz1090_pdc_pinctrl_init(void)
  840. {
  841. return platform_driver_register(&tz1090_pdc_pinctrl_driver);
  842. }
  843. arch_initcall(tz1090_pdc_pinctrl_init);
  844. static void __exit tz1090_pdc_pinctrl_exit(void)
  845. {
  846. platform_driver_unregister(&tz1090_pdc_pinctrl_driver);
  847. }
  848. module_exit(tz1090_pdc_pinctrl_exit);
  849. MODULE_AUTHOR("Imagination Technologies Ltd.");
  850. MODULE_DESCRIPTION("Toumaz Xenif TZ1090 PDC pinctrl driver");
  851. MODULE_LICENSE("GPL v2");
  852. MODULE_DEVICE_TABLE(of, tz1090_pdc_pinctrl_of_match);