pinctrl-tz1090-pdc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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, &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_enable(struct pinctrl_dev *pctldev,
  471. unsigned int function, unsigned int group)
  472. {
  473. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  474. const struct tz1090_pdc_pingroup *grp = &tz1090_pdc_groups[group];
  475. dev_dbg(pctldev->dev, "%s(func=%u (%s), group=%u (%s))\n",
  476. __func__,
  477. function, tz1090_pdc_functions[function].name,
  478. group, tz1090_pdc_groups[group].name);
  479. /* is it even a mux? */
  480. if (grp->drv)
  481. return -EINVAL;
  482. /* does this group even control the function? */
  483. if (function != grp->func)
  484. return -EINVAL;
  485. /* record the pin being muxed and update mux bit */
  486. spin_lock(&pmx->lock);
  487. pmx->mux_en |= BIT(grp->pins[0]);
  488. tz1090_pdc_pinctrl_mux(pmx, grp);
  489. spin_unlock(&pmx->lock);
  490. return 0;
  491. }
  492. static void tz1090_pdc_pinctrl_disable(struct pinctrl_dev *pctldev,
  493. unsigned int function,
  494. unsigned int group)
  495. {
  496. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  497. const struct tz1090_pdc_pingroup *grp = &tz1090_pdc_groups[group];
  498. dev_dbg(pctldev->dev, "%s(func=%u (%s), group=%u (%s))\n",
  499. __func__,
  500. function, tz1090_pdc_functions[function].name,
  501. group, tz1090_pdc_groups[group].name);
  502. /* is it even a mux? */
  503. if (grp->drv)
  504. return;
  505. /* does this group even control the function? */
  506. if (function != grp->func)
  507. return;
  508. /* record the pin being unmuxed and update mux bit */
  509. spin_lock(&pmx->lock);
  510. pmx->mux_en &= ~BIT(grp->pins[0]);
  511. tz1090_pdc_pinctrl_mux(pmx, grp);
  512. spin_unlock(&pmx->lock);
  513. }
  514. static const struct tz1090_pdc_pingroup *find_mux_group(
  515. struct tz1090_pdc_pmx *pmx,
  516. unsigned int pin)
  517. {
  518. const struct tz1090_pdc_pingroup *grp;
  519. unsigned int group;
  520. grp = tz1090_pdc_groups;
  521. for (group = 0; group < ARRAY_SIZE(tz1090_pdc_groups); ++group, ++grp) {
  522. /* only match muxes */
  523. if (grp->drv)
  524. continue;
  525. /* with a matching pin */
  526. if (grp->pins[0] == pin)
  527. return grp;
  528. }
  529. return NULL;
  530. }
  531. static int tz1090_pdc_pinctrl_gpio_request_enable(
  532. struct pinctrl_dev *pctldev,
  533. struct pinctrl_gpio_range *range,
  534. unsigned int pin)
  535. {
  536. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  537. const struct tz1090_pdc_pingroup *grp = find_mux_group(pmx, pin);
  538. if (grp) {
  539. /* record the pin in GPIO use and update mux bit */
  540. spin_lock(&pmx->lock);
  541. pmx->gpio_en |= BIT(pin);
  542. tz1090_pdc_pinctrl_mux(pmx, grp);
  543. spin_unlock(&pmx->lock);
  544. }
  545. return 0;
  546. }
  547. static void tz1090_pdc_pinctrl_gpio_disable_free(
  548. struct pinctrl_dev *pctldev,
  549. struct pinctrl_gpio_range *range,
  550. unsigned int pin)
  551. {
  552. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  553. const struct tz1090_pdc_pingroup *grp = find_mux_group(pmx, pin);
  554. if (grp) {
  555. /* record the pin not in GPIO use and update mux bit */
  556. spin_lock(&pmx->lock);
  557. pmx->gpio_en &= ~BIT(pin);
  558. tz1090_pdc_pinctrl_mux(pmx, grp);
  559. spin_unlock(&pmx->lock);
  560. }
  561. }
  562. static struct pinmux_ops tz1090_pdc_pinmux_ops = {
  563. .get_functions_count = tz1090_pdc_pinctrl_get_funcs_count,
  564. .get_function_name = tz1090_pdc_pinctrl_get_func_name,
  565. .get_function_groups = tz1090_pdc_pinctrl_get_func_groups,
  566. .enable = tz1090_pdc_pinctrl_enable,
  567. .disable = tz1090_pdc_pinctrl_disable,
  568. .gpio_request_enable = tz1090_pdc_pinctrl_gpio_request_enable,
  569. .gpio_disable_free = tz1090_pdc_pinctrl_gpio_disable_free,
  570. };
  571. /*
  572. * Pin config operations
  573. */
  574. static int tz1090_pdc_pinconf_reg(struct pinctrl_dev *pctldev,
  575. unsigned int pin,
  576. enum pin_config_param param,
  577. bool report_err,
  578. u32 *reg, u32 *width, u32 *mask, u32 *shift,
  579. u32 *val)
  580. {
  581. /* Find information about parameter's register */
  582. switch (param) {
  583. case PIN_CONFIG_BIAS_DISABLE:
  584. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  585. *val = REG_PU_PD_TRISTATE;
  586. break;
  587. case PIN_CONFIG_BIAS_PULL_UP:
  588. *val = REG_PU_PD_UP;
  589. break;
  590. case PIN_CONFIG_BIAS_PULL_DOWN:
  591. *val = REG_PU_PD_DOWN;
  592. break;
  593. case PIN_CONFIG_BIAS_BUS_HOLD:
  594. *val = REG_PU_PD_REPEATER;
  595. break;
  596. default:
  597. return -ENOTSUPP;
  598. };
  599. /* Only input bias parameters supported */
  600. *reg = REG_GPIO_CONTROL2;
  601. *shift = REG_GPIO_CONTROL2_PU_PD_S + pin*2;
  602. *width = 2;
  603. /* Calculate field information */
  604. *mask = (BIT(*width) - 1) << *shift;
  605. return 0;
  606. }
  607. static int tz1090_pdc_pinconf_get(struct pinctrl_dev *pctldev,
  608. unsigned int pin, unsigned long *config)
  609. {
  610. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  611. enum pin_config_param param = pinconf_to_config_param(*config);
  612. int ret;
  613. u32 reg, width, mask, shift, val, tmp, arg;
  614. /* Get register information */
  615. ret = tz1090_pdc_pinconf_reg(pctldev, pin, param, true,
  616. &reg, &width, &mask, &shift, &val);
  617. if (ret < 0)
  618. return ret;
  619. /* Extract field from register */
  620. tmp = pmx_read(pmx, reg);
  621. arg = ((tmp & mask) >> shift) == val;
  622. /* Config not active */
  623. if (!arg)
  624. return -EINVAL;
  625. /* And pack config */
  626. *config = pinconf_to_config_packed(param, arg);
  627. return 0;
  628. }
  629. static int tz1090_pdc_pinconf_set(struct pinctrl_dev *pctldev,
  630. unsigned int pin, unsigned long *configs,
  631. unsigned num_configs)
  632. {
  633. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  634. enum pin_config_param param;
  635. unsigned int arg;
  636. int ret;
  637. u32 reg, width, mask, shift, val, tmp;
  638. unsigned long flags;
  639. int i;
  640. for (i = 0; i < num_configs; i++) {
  641. param = pinconf_to_config_param(configs[i]);
  642. arg = pinconf_to_config_argument(configs[i]);
  643. dev_dbg(pctldev->dev, "%s(pin=%s, config=%#lx)\n",
  644. __func__, tz1090_pdc_pins[pin].name, configs[i]);
  645. /* Get register information */
  646. ret = tz1090_pdc_pinconf_reg(pctldev, pin, param, true,
  647. &reg, &width, &mask, &shift, &val);
  648. if (ret < 0)
  649. return ret;
  650. /* Unpack argument and range check it */
  651. if (arg > 1) {
  652. dev_dbg(pctldev->dev, "%s: arg %u out of range\n",
  653. __func__, arg);
  654. return -EINVAL;
  655. }
  656. /* Write register field */
  657. __global_lock2(flags);
  658. tmp = pmx_read(pmx, reg);
  659. tmp &= ~mask;
  660. if (arg)
  661. tmp |= val << shift;
  662. pmx_write(pmx, tmp, reg);
  663. __global_unlock2(flags);
  664. } /* for each config */
  665. return 0;
  666. }
  667. static const int tz1090_pdc_boolean_map[] = {
  668. [0] = -EINVAL,
  669. [1] = 1,
  670. };
  671. static const int tz1090_pdc_dr_map[] = {
  672. [REG_DR_2mA] = 2,
  673. [REG_DR_4mA] = 4,
  674. [REG_DR_8mA] = 8,
  675. [REG_DR_12mA] = 12,
  676. };
  677. static int tz1090_pdc_pinconf_group_reg(struct pinctrl_dev *pctldev,
  678. const struct tz1090_pdc_pingroup *g,
  679. enum pin_config_param param,
  680. bool report_err, u32 *reg, u32 *width,
  681. u32 *mask, u32 *shift, const int **map)
  682. {
  683. /* Drive configuration applies in groups, but not to all groups. */
  684. if (!g->drv) {
  685. if (report_err)
  686. dev_dbg(pctldev->dev,
  687. "%s: group %s has no drive control\n",
  688. __func__, g->name);
  689. return -ENOTSUPP;
  690. }
  691. /* Find information about drive parameter's register */
  692. *reg = REG_GPIO_CONTROL2;
  693. switch (param) {
  694. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  695. *shift = REG_GPIO_CONTROL2_PDC_SCHMITT_S;
  696. *width = 1;
  697. *map = tz1090_pdc_boolean_map;
  698. break;
  699. case PIN_CONFIG_DRIVE_STRENGTH:
  700. *shift = REG_GPIO_CONTROL2_PDC_DR_S;
  701. *width = 2;
  702. *map = tz1090_pdc_dr_map;
  703. break;
  704. case PIN_CONFIG_LOW_POWER_MODE:
  705. *shift = REG_GPIO_CONTROL2_PDC_POS_S;
  706. *width = 1;
  707. *map = tz1090_pdc_boolean_map;
  708. break;
  709. default:
  710. return -ENOTSUPP;
  711. };
  712. /* Calculate field information */
  713. *mask = (BIT(*width) - 1) << *shift;
  714. return 0;
  715. }
  716. static int tz1090_pdc_pinconf_group_get(struct pinctrl_dev *pctldev,
  717. unsigned int group,
  718. unsigned long *config)
  719. {
  720. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  721. const struct tz1090_pdc_pingroup *g = &tz1090_pdc_groups[group];
  722. enum pin_config_param param = pinconf_to_config_param(*config);
  723. int ret, arg;
  724. u32 reg, width, mask, shift, val;
  725. const int *map;
  726. /* Get register information */
  727. ret = tz1090_pdc_pinconf_group_reg(pctldev, g, param, true,
  728. &reg, &width, &mask, &shift, &map);
  729. if (ret < 0)
  730. return ret;
  731. /* Extract field from register */
  732. val = pmx_read(pmx, reg);
  733. arg = map[(val & mask) >> shift];
  734. if (arg < 0)
  735. return arg;
  736. /* And pack config */
  737. *config = pinconf_to_config_packed(param, arg);
  738. return 0;
  739. }
  740. static int tz1090_pdc_pinconf_group_set(struct pinctrl_dev *pctldev,
  741. unsigned int group,
  742. unsigned long *configs,
  743. unsigned num_configs)
  744. {
  745. struct tz1090_pdc_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
  746. const struct tz1090_pdc_pingroup *g = &tz1090_pdc_groups[group];
  747. enum pin_config_param param;
  748. const unsigned int *pit;
  749. unsigned int i;
  750. int ret, arg;
  751. u32 reg, width, mask, shift, val;
  752. unsigned long flags;
  753. const int *map;
  754. int j;
  755. for (j = 0; j < num_configs; j++) {
  756. param = pinconf_to_config_param(configs[j]);
  757. dev_dbg(pctldev->dev, "%s(group=%s, config=%#lx)\n",
  758. __func__, g->name, configs[j]);
  759. /* Get register information */
  760. ret = tz1090_pdc_pinconf_group_reg(pctldev, g, param, true,
  761. &reg, &width, &mask, &shift,
  762. &map);
  763. if (ret < 0) {
  764. /*
  765. * Maybe we're trying to set a per-pin configuration
  766. * of a group, so do the pins one by one. This is
  767. * mainly as a convenience.
  768. */
  769. for (i = 0, pit = g->pins; i < g->npins; ++i, ++pit) {
  770. ret = tz1090_pdc_pinconf_set(pctldev, *pit,
  771. configs, num_configs);
  772. if (ret)
  773. return ret;
  774. }
  775. return 0;
  776. }
  777. /* Unpack argument and map it to register value */
  778. arg = pinconf_to_config_argument(configs[j]);
  779. for (i = 0; i < BIT(width); ++i) {
  780. if (map[i] == arg || (map[i] == -EINVAL && !arg)) {
  781. /* Write register field */
  782. __global_lock2(flags);
  783. val = pmx_read(pmx, reg);
  784. val &= ~mask;
  785. val |= i << shift;
  786. pmx_write(pmx, val, reg);
  787. __global_unlock2(flags);
  788. goto next_config;
  789. }
  790. }
  791. dev_dbg(pctldev->dev, "%s: arg %u not supported\n",
  792. __func__, arg);
  793. return 0;
  794. next_config:
  795. ;
  796. } /* for each config */
  797. return 0;
  798. }
  799. static struct pinconf_ops tz1090_pdc_pinconf_ops = {
  800. .is_generic = true,
  801. .pin_config_get = tz1090_pdc_pinconf_get,
  802. .pin_config_set = tz1090_pdc_pinconf_set,
  803. .pin_config_group_get = tz1090_pdc_pinconf_group_get,
  804. .pin_config_group_set = tz1090_pdc_pinconf_group_set,
  805. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  806. };
  807. /*
  808. * Pin control driver setup
  809. */
  810. static struct pinctrl_desc tz1090_pdc_pinctrl_desc = {
  811. .pctlops = &tz1090_pdc_pinctrl_ops,
  812. .pmxops = &tz1090_pdc_pinmux_ops,
  813. .confops = &tz1090_pdc_pinconf_ops,
  814. .owner = THIS_MODULE,
  815. };
  816. static int tz1090_pdc_pinctrl_probe(struct platform_device *pdev)
  817. {
  818. struct tz1090_pdc_pmx *pmx;
  819. struct resource *res;
  820. pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
  821. if (!pmx) {
  822. dev_err(&pdev->dev, "Can't alloc tz1090_pdc_pmx\n");
  823. return -ENOMEM;
  824. }
  825. pmx->dev = &pdev->dev;
  826. spin_lock_init(&pmx->lock);
  827. tz1090_pdc_pinctrl_desc.name = dev_name(&pdev->dev);
  828. tz1090_pdc_pinctrl_desc.pins = tz1090_pdc_pins;
  829. tz1090_pdc_pinctrl_desc.npins = ARRAY_SIZE(tz1090_pdc_pins);
  830. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  831. pmx->regs = devm_ioremap_resource(&pdev->dev, res);
  832. if (IS_ERR(pmx->regs))
  833. return PTR_ERR(pmx->regs);
  834. pmx->pctl = pinctrl_register(&tz1090_pdc_pinctrl_desc, &pdev->dev, pmx);
  835. if (!pmx->pctl) {
  836. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  837. return -ENODEV;
  838. }
  839. platform_set_drvdata(pdev, pmx);
  840. dev_info(&pdev->dev, "TZ1090 PDC pinctrl driver initialised\n");
  841. return 0;
  842. }
  843. static int tz1090_pdc_pinctrl_remove(struct platform_device *pdev)
  844. {
  845. struct tz1090_pdc_pmx *pmx = platform_get_drvdata(pdev);
  846. pinctrl_unregister(pmx->pctl);
  847. return 0;
  848. }
  849. static struct of_device_id tz1090_pdc_pinctrl_of_match[] = {
  850. { .compatible = "img,tz1090-pdc-pinctrl", },
  851. { },
  852. };
  853. static struct platform_driver tz1090_pdc_pinctrl_driver = {
  854. .driver = {
  855. .name = "tz1090-pdc-pinctrl",
  856. .owner = THIS_MODULE,
  857. .of_match_table = tz1090_pdc_pinctrl_of_match,
  858. },
  859. .probe = tz1090_pdc_pinctrl_probe,
  860. .remove = tz1090_pdc_pinctrl_remove,
  861. };
  862. static int __init tz1090_pdc_pinctrl_init(void)
  863. {
  864. return platform_driver_register(&tz1090_pdc_pinctrl_driver);
  865. }
  866. arch_initcall(tz1090_pdc_pinctrl_init);
  867. static void __exit tz1090_pdc_pinctrl_exit(void)
  868. {
  869. platform_driver_unregister(&tz1090_pdc_pinctrl_driver);
  870. }
  871. module_exit(tz1090_pdc_pinctrl_exit);
  872. MODULE_AUTHOR("Imagination Technologies Ltd.");
  873. MODULE_DESCRIPTION("Toumaz Xenif TZ1090 PDC pinctrl driver");
  874. MODULE_LICENSE("GPL v2");
  875. MODULE_DEVICE_TABLE(of, tz1090_pdc_pinctrl_of_match);