core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * SuperH Pin Function Controller support.
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. * Copyright (C) 2009 - 2012 Paul Mundt
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #define DRV_NAME "sh-pfc"
  12. #include <linux/bitops.h>
  13. #include <linux/err.h>
  14. #include <linux/errno.h>
  15. #include <linux/io.h>
  16. #include <linux/ioport.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pinctrl/machine.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include "core.h"
  23. static int sh_pfc_ioremap(struct sh_pfc *pfc, struct platform_device *pdev)
  24. {
  25. struct resource *res;
  26. int k;
  27. if (pdev->num_resources == 0)
  28. return -EINVAL;
  29. pfc->window = devm_kzalloc(pfc->dev, pdev->num_resources *
  30. sizeof(*pfc->window), GFP_NOWAIT);
  31. if (!pfc->window)
  32. return -ENOMEM;
  33. pfc->num_windows = pdev->num_resources;
  34. for (k = 0, res = pdev->resource; k < pdev->num_resources; k++, res++) {
  35. WARN_ON(resource_type(res) != IORESOURCE_MEM);
  36. pfc->window[k].phys = res->start;
  37. pfc->window[k].size = resource_size(res);
  38. pfc->window[k].virt = devm_ioremap_nocache(pfc->dev, res->start,
  39. resource_size(res));
  40. if (!pfc->window[k].virt)
  41. return -ENOMEM;
  42. }
  43. return 0;
  44. }
  45. static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc,
  46. unsigned long address)
  47. {
  48. struct sh_pfc_window *window;
  49. unsigned int i;
  50. /* scan through physical windows and convert address */
  51. for (i = 0; i < pfc->num_windows; i++) {
  52. window = pfc->window + i;
  53. if (address < window->phys)
  54. continue;
  55. if (address >= (window->phys + window->size))
  56. continue;
  57. return window->virt + (address - window->phys);
  58. }
  59. BUG();
  60. return NULL;
  61. }
  62. int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
  63. {
  64. unsigned int offset;
  65. unsigned int i;
  66. if (pfc->info->ranges == NULL)
  67. return pin;
  68. for (i = 0, offset = 0; i < pfc->info->nr_ranges; ++i) {
  69. const struct pinmux_range *range = &pfc->info->ranges[i];
  70. if (pin <= range->end)
  71. return pin >= range->begin
  72. ? offset + pin - range->begin : -1;
  73. offset += range->end - range->begin + 1;
  74. }
  75. return -EINVAL;
  76. }
  77. static int sh_pfc_enum_in_range(pinmux_enum_t enum_id,
  78. const struct pinmux_range *r)
  79. {
  80. if (enum_id < r->begin)
  81. return 0;
  82. if (enum_id > r->end)
  83. return 0;
  84. return 1;
  85. }
  86. unsigned long sh_pfc_read_raw_reg(void __iomem *mapped_reg,
  87. unsigned long reg_width)
  88. {
  89. switch (reg_width) {
  90. case 8:
  91. return ioread8(mapped_reg);
  92. case 16:
  93. return ioread16(mapped_reg);
  94. case 32:
  95. return ioread32(mapped_reg);
  96. }
  97. BUG();
  98. return 0;
  99. }
  100. void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned long reg_width,
  101. unsigned long data)
  102. {
  103. switch (reg_width) {
  104. case 8:
  105. iowrite8(data, mapped_reg);
  106. return;
  107. case 16:
  108. iowrite16(data, mapped_reg);
  109. return;
  110. case 32:
  111. iowrite32(data, mapped_reg);
  112. return;
  113. }
  114. BUG();
  115. }
  116. static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
  117. const struct pinmux_cfg_reg *crp,
  118. unsigned long in_pos,
  119. void __iomem **mapped_regp,
  120. unsigned long *maskp,
  121. unsigned long *posp)
  122. {
  123. int k;
  124. *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
  125. if (crp->field_width) {
  126. *maskp = (1 << crp->field_width) - 1;
  127. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  128. } else {
  129. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  130. *posp = crp->reg_width;
  131. for (k = 0; k <= in_pos; k++)
  132. *posp -= crp->var_field_width[k];
  133. }
  134. }
  135. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  136. const struct pinmux_cfg_reg *crp,
  137. unsigned long field, unsigned long value)
  138. {
  139. void __iomem *mapped_reg;
  140. unsigned long mask, pos, data;
  141. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  142. dev_dbg(pfc->dev, "write_reg addr = %lx, value = %ld, field = %ld, "
  143. "r_width = %ld, f_width = %ld\n",
  144. crp->reg, value, field, crp->reg_width, crp->field_width);
  145. mask = ~(mask << pos);
  146. value = value << pos;
  147. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  148. data &= mask;
  149. data |= value;
  150. if (pfc->info->unlock_reg)
  151. sh_pfc_write_raw_reg(
  152. sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
  153. ~data);
  154. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  155. }
  156. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
  157. const struct pinmux_cfg_reg **crp, int *fieldp,
  158. int *valuep)
  159. {
  160. const struct pinmux_cfg_reg *config_reg;
  161. unsigned long r_width, f_width, curr_width, ncomb;
  162. int k, m, n, pos, bit_pos;
  163. k = 0;
  164. while (1) {
  165. config_reg = pfc->info->cfg_regs + k;
  166. r_width = config_reg->reg_width;
  167. f_width = config_reg->field_width;
  168. if (!r_width)
  169. break;
  170. pos = 0;
  171. m = 0;
  172. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  173. if (f_width)
  174. curr_width = f_width;
  175. else
  176. curr_width = config_reg->var_field_width[m];
  177. ncomb = 1 << curr_width;
  178. for (n = 0; n < ncomb; n++) {
  179. if (config_reg->enum_ids[pos + n] == enum_id) {
  180. *crp = config_reg;
  181. *fieldp = m;
  182. *valuep = n;
  183. return 0;
  184. }
  185. }
  186. pos += ncomb;
  187. m++;
  188. }
  189. k++;
  190. }
  191. return -EINVAL;
  192. }
  193. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, pinmux_enum_t mark, int pos,
  194. pinmux_enum_t *enum_idp)
  195. {
  196. const pinmux_enum_t *data = pfc->info->gpio_data;
  197. int k;
  198. if (pos) {
  199. *enum_idp = data[pos + 1];
  200. return pos + 1;
  201. }
  202. for (k = 0; k < pfc->info->gpio_data_size; k++) {
  203. if (data[k] == mark) {
  204. *enum_idp = data[k + 1];
  205. return k + 1;
  206. }
  207. }
  208. dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
  209. mark);
  210. return -EINVAL;
  211. }
  212. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
  213. {
  214. const struct pinmux_cfg_reg *cr = NULL;
  215. pinmux_enum_t enum_id;
  216. const struct pinmux_range *range;
  217. int in_range, pos, field, value;
  218. int ret;
  219. switch (pinmux_type) {
  220. case PINMUX_TYPE_FUNCTION:
  221. range = NULL;
  222. break;
  223. case PINMUX_TYPE_OUTPUT:
  224. range = &pfc->info->output;
  225. break;
  226. case PINMUX_TYPE_INPUT:
  227. range = &pfc->info->input;
  228. break;
  229. case PINMUX_TYPE_INPUT_PULLUP:
  230. range = &pfc->info->input_pu;
  231. break;
  232. case PINMUX_TYPE_INPUT_PULLDOWN:
  233. range = &pfc->info->input_pd;
  234. break;
  235. default:
  236. return -EINVAL;
  237. }
  238. pos = 0;
  239. enum_id = 0;
  240. field = 0;
  241. value = 0;
  242. while (1) {
  243. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  244. if (pos < 0)
  245. return pos;
  246. if (!enum_id)
  247. break;
  248. /* first check if this is a function enum */
  249. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  250. if (!in_range) {
  251. /* not a function enum */
  252. if (range) {
  253. /*
  254. * other range exists, so this pin is
  255. * a regular GPIO pin that now is being
  256. * bound to a specific direction.
  257. *
  258. * for this case we only allow function enums
  259. * and the enums that match the other range.
  260. */
  261. in_range = sh_pfc_enum_in_range(enum_id, range);
  262. /*
  263. * special case pass through for fixed
  264. * input-only or output-only pins without
  265. * function enum register association.
  266. */
  267. if (in_range && enum_id == range->force)
  268. continue;
  269. } else {
  270. /*
  271. * no other range exists, so this pin
  272. * must then be of the function type.
  273. *
  274. * allow function type pins to select
  275. * any combination of function/in/out
  276. * in their MARK lists.
  277. */
  278. in_range = 1;
  279. }
  280. }
  281. if (!in_range)
  282. continue;
  283. ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
  284. if (ret < 0)
  285. return ret;
  286. sh_pfc_write_config_reg(pfc, cr, field, value);
  287. }
  288. return 0;
  289. }
  290. static int sh_pfc_probe(struct platform_device *pdev)
  291. {
  292. const struct sh_pfc_soc_info *info;
  293. struct sh_pfc *pfc;
  294. int ret;
  295. info = pdev->id_entry->driver_data
  296. ? (void *)pdev->id_entry->driver_data : pdev->dev.platform_data;
  297. if (info == NULL)
  298. return -ENODEV;
  299. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  300. if (pfc == NULL)
  301. return -ENOMEM;
  302. pfc->info = info;
  303. pfc->dev = &pdev->dev;
  304. ret = sh_pfc_ioremap(pfc, pdev);
  305. if (unlikely(ret < 0))
  306. return ret;
  307. spin_lock_init(&pfc->lock);
  308. pinctrl_provide_dummies();
  309. /*
  310. * Initialize pinctrl bindings first
  311. */
  312. ret = sh_pfc_register_pinctrl(pfc);
  313. if (unlikely(ret != 0))
  314. return ret;
  315. #ifdef CONFIG_GPIO_SH_PFC
  316. /*
  317. * Then the GPIO chip
  318. */
  319. ret = sh_pfc_register_gpiochip(pfc);
  320. if (unlikely(ret != 0)) {
  321. /*
  322. * If the GPIO chip fails to come up we still leave the
  323. * PFC state as it is, given that there are already
  324. * extant users of it that have succeeded by this point.
  325. */
  326. dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
  327. }
  328. #endif
  329. platform_set_drvdata(pdev, pfc);
  330. dev_info(pfc->dev, "%s support registered\n", info->name);
  331. return 0;
  332. }
  333. static int sh_pfc_remove(struct platform_device *pdev)
  334. {
  335. struct sh_pfc *pfc = platform_get_drvdata(pdev);
  336. #ifdef CONFIG_GPIO_SH_PFC
  337. sh_pfc_unregister_gpiochip(pfc);
  338. #endif
  339. sh_pfc_unregister_pinctrl(pfc);
  340. platform_set_drvdata(pdev, NULL);
  341. return 0;
  342. }
  343. static const struct platform_device_id sh_pfc_id_table[] = {
  344. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  345. { "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
  346. #endif
  347. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  348. { "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
  349. #endif
  350. #ifdef CONFIG_PINCTRL_PFC_SH7203
  351. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  352. #endif
  353. #ifdef CONFIG_PINCTRL_PFC_SH7264
  354. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  355. #endif
  356. #ifdef CONFIG_PINCTRL_PFC_SH7269
  357. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  358. #endif
  359. #ifdef CONFIG_PINCTRL_PFC_SH7372
  360. { "pfc-sh7372", (kernel_ulong_t)&sh7372_pinmux_info },
  361. #endif
  362. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  363. { "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
  364. #endif
  365. #ifdef CONFIG_PINCTRL_PFC_SH7720
  366. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  367. #endif
  368. #ifdef CONFIG_PINCTRL_PFC_SH7722
  369. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  370. #endif
  371. #ifdef CONFIG_PINCTRL_PFC_SH7723
  372. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  373. #endif
  374. #ifdef CONFIG_PINCTRL_PFC_SH7724
  375. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  376. #endif
  377. #ifdef CONFIG_PINCTRL_PFC_SH7734
  378. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  379. #endif
  380. #ifdef CONFIG_PINCTRL_PFC_SH7757
  381. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  382. #endif
  383. #ifdef CONFIG_PINCTRL_PFC_SH7785
  384. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  385. #endif
  386. #ifdef CONFIG_PINCTRL_PFC_SH7786
  387. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  388. #endif
  389. #ifdef CONFIG_PINCTRL_PFC_SHX3
  390. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  391. #endif
  392. { "sh-pfc", 0 },
  393. { },
  394. };
  395. MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
  396. static struct platform_driver sh_pfc_driver = {
  397. .probe = sh_pfc_probe,
  398. .remove = sh_pfc_remove,
  399. .id_table = sh_pfc_id_table,
  400. .driver = {
  401. .name = DRV_NAME,
  402. .owner = THIS_MODULE,
  403. },
  404. };
  405. static int __init sh_pfc_init(void)
  406. {
  407. return platform_driver_register(&sh_pfc_driver);
  408. }
  409. postcore_initcall(sh_pfc_init);
  410. static void __exit sh_pfc_exit(void)
  411. {
  412. platform_driver_unregister(&sh_pfc_driver);
  413. }
  414. module_exit(sh_pfc_exit);
  415. MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
  416. MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
  417. MODULE_LICENSE("GPL v2");