core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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_GPIO:
  221. case PINMUX_TYPE_FUNCTION:
  222. range = NULL;
  223. break;
  224. case PINMUX_TYPE_OUTPUT:
  225. range = &pfc->info->output;
  226. break;
  227. case PINMUX_TYPE_INPUT:
  228. range = &pfc->info->input;
  229. break;
  230. case PINMUX_TYPE_INPUT_PULLUP:
  231. range = &pfc->info->input_pu;
  232. break;
  233. case PINMUX_TYPE_INPUT_PULLDOWN:
  234. range = &pfc->info->input_pd;
  235. break;
  236. default:
  237. return -EINVAL;
  238. }
  239. pos = 0;
  240. enum_id = 0;
  241. field = 0;
  242. value = 0;
  243. /* Iterate over all the configuration fields we need to update. */
  244. while (1) {
  245. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  246. if (pos < 0)
  247. return pos;
  248. if (!enum_id)
  249. break;
  250. /* Check if the configuration field selects a function. If it
  251. * doesn't, skip the field if it's not applicable to the
  252. * requested pinmux type.
  253. */
  254. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  255. if (!in_range) {
  256. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  257. /* Functions are allowed to modify all
  258. * fields.
  259. */
  260. in_range = 1;
  261. } else if (pinmux_type != PINMUX_TYPE_GPIO) {
  262. /* Input/output types can only modify fields
  263. * that correspond to their respective ranges.
  264. */
  265. in_range = sh_pfc_enum_in_range(enum_id, range);
  266. /*
  267. * special case pass through for fixed
  268. * input-only or output-only pins without
  269. * function enum register association.
  270. */
  271. if (in_range && enum_id == range->force)
  272. continue;
  273. }
  274. /* GPIOs are only allowed to modify function fields. */
  275. }
  276. if (!in_range)
  277. continue;
  278. ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
  279. if (ret < 0)
  280. return ret;
  281. sh_pfc_write_config_reg(pfc, cr, field, value);
  282. }
  283. return 0;
  284. }
  285. static int sh_pfc_probe(struct platform_device *pdev)
  286. {
  287. const struct sh_pfc_soc_info *info;
  288. struct sh_pfc *pfc;
  289. int ret;
  290. info = pdev->id_entry->driver_data
  291. ? (void *)pdev->id_entry->driver_data : pdev->dev.platform_data;
  292. if (info == NULL)
  293. return -ENODEV;
  294. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  295. if (pfc == NULL)
  296. return -ENOMEM;
  297. pfc->info = info;
  298. pfc->dev = &pdev->dev;
  299. ret = sh_pfc_ioremap(pfc, pdev);
  300. if (unlikely(ret < 0))
  301. return ret;
  302. spin_lock_init(&pfc->lock);
  303. pinctrl_provide_dummies();
  304. /*
  305. * Initialize pinctrl bindings first
  306. */
  307. ret = sh_pfc_register_pinctrl(pfc);
  308. if (unlikely(ret != 0))
  309. return ret;
  310. #ifdef CONFIG_GPIO_SH_PFC
  311. /*
  312. * Then the GPIO chip
  313. */
  314. ret = sh_pfc_register_gpiochip(pfc);
  315. if (unlikely(ret != 0)) {
  316. /*
  317. * If the GPIO chip fails to come up we still leave the
  318. * PFC state as it is, given that there are already
  319. * extant users of it that have succeeded by this point.
  320. */
  321. dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
  322. }
  323. #endif
  324. platform_set_drvdata(pdev, pfc);
  325. dev_info(pfc->dev, "%s support registered\n", info->name);
  326. return 0;
  327. }
  328. static int sh_pfc_remove(struct platform_device *pdev)
  329. {
  330. struct sh_pfc *pfc = platform_get_drvdata(pdev);
  331. #ifdef CONFIG_GPIO_SH_PFC
  332. sh_pfc_unregister_gpiochip(pfc);
  333. #endif
  334. sh_pfc_unregister_pinctrl(pfc);
  335. platform_set_drvdata(pdev, NULL);
  336. return 0;
  337. }
  338. static const struct platform_device_id sh_pfc_id_table[] = {
  339. #ifdef CONFIG_PINCTRL_PFC_R8A73A4
  340. { "pfc-r8a73a4", (kernel_ulong_t)&r8a73a4_pinmux_info },
  341. #endif
  342. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  343. { "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
  344. #endif
  345. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  346. { "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
  347. #endif
  348. #ifdef CONFIG_PINCTRL_PFC_SH7203
  349. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  350. #endif
  351. #ifdef CONFIG_PINCTRL_PFC_SH7264
  352. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  353. #endif
  354. #ifdef CONFIG_PINCTRL_PFC_SH7269
  355. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  356. #endif
  357. #ifdef CONFIG_PINCTRL_PFC_SH7372
  358. { "pfc-sh7372", (kernel_ulong_t)&sh7372_pinmux_info },
  359. #endif
  360. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  361. { "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
  362. #endif
  363. #ifdef CONFIG_PINCTRL_PFC_SH7720
  364. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  365. #endif
  366. #ifdef CONFIG_PINCTRL_PFC_SH7722
  367. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  368. #endif
  369. #ifdef CONFIG_PINCTRL_PFC_SH7723
  370. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  371. #endif
  372. #ifdef CONFIG_PINCTRL_PFC_SH7724
  373. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  374. #endif
  375. #ifdef CONFIG_PINCTRL_PFC_SH7734
  376. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  377. #endif
  378. #ifdef CONFIG_PINCTRL_PFC_SH7757
  379. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  380. #endif
  381. #ifdef CONFIG_PINCTRL_PFC_SH7785
  382. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  383. #endif
  384. #ifdef CONFIG_PINCTRL_PFC_SH7786
  385. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  386. #endif
  387. #ifdef CONFIG_PINCTRL_PFC_SHX3
  388. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  389. #endif
  390. { "sh-pfc", 0 },
  391. { },
  392. };
  393. MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
  394. static struct platform_driver sh_pfc_driver = {
  395. .probe = sh_pfc_probe,
  396. .remove = sh_pfc_remove,
  397. .id_table = sh_pfc_id_table,
  398. .driver = {
  399. .name = DRV_NAME,
  400. .owner = THIS_MODULE,
  401. },
  402. };
  403. static int __init sh_pfc_init(void)
  404. {
  405. return platform_driver_register(&sh_pfc_driver);
  406. }
  407. postcore_initcall(sh_pfc_init);
  408. static void __exit sh_pfc_exit(void)
  409. {
  410. platform_driver_unregister(&sh_pfc_driver);
  411. }
  412. module_exit(sh_pfc_exit);
  413. MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
  414. MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
  415. MODULE_LICENSE("GPL v2");