gpio-sch311x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * GPIO driver for the SMSC SCH311x Super-I/O chips
  3. *
  4. * Copyright (C) 2013 Bruno Randolf <br1@einfach.org>
  5. *
  6. * SuperIO functions and chip detection:
  7. * (c) Copyright 2008 Wim Van Sebroeck <wim@iguana.be>.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/gpio.h>
  19. #include <linux/bitops.h>
  20. #include <linux/io.h>
  21. #define DRV_NAME "gpio-sch311x"
  22. #define SCH311X_GPIO_CONF_OUT 0x00
  23. #define SCH311X_GPIO_CONF_IN 0x01
  24. #define SCH311X_GPIO_CONF_INVERT 0x02
  25. #define SCH311X_GPIO_CONF_OPEN_DRAIN 0x80
  26. #define SIO_CONFIG_KEY_ENTER 0x55
  27. #define SIO_CONFIG_KEY_EXIT 0xaa
  28. #define GP1 0x4b
  29. static int sch311x_ioports[] = { 0x2e, 0x4e, 0x162e, 0x164e };
  30. static struct platform_device *sch311x_gpio_pdev;
  31. struct sch311x_pdev_data { /* platform device data */
  32. unsigned short runtime_reg; /* runtime register base address */
  33. };
  34. struct sch311x_gpio_block { /* one GPIO block runtime data */
  35. struct gpio_chip chip;
  36. unsigned short data_reg; /* from definition below */
  37. unsigned short *config_regs; /* pointer to definition below */
  38. unsigned short runtime_reg; /* runtime register */
  39. spinlock_t lock; /* lock for this GPIO block */
  40. };
  41. struct sch311x_gpio_priv { /* driver private data */
  42. struct sch311x_gpio_block blocks[6];
  43. };
  44. struct sch311x_gpio_block_def { /* register address definitions */
  45. unsigned short data_reg;
  46. unsigned short config_regs[8];
  47. unsigned short base;
  48. };
  49. /* Note: some GPIOs are not available, these are marked with 0x00 */
  50. static struct sch311x_gpio_block_def sch311x_gpio_blocks[] = {
  51. {
  52. .data_reg = 0x4b, /* GP1 */
  53. .config_regs = {0x23, 0x24, 0x25, 0x26, 0x27, 0x29, 0x2a, 0x2b},
  54. .base = 10,
  55. },
  56. {
  57. .data_reg = 0x4c, /* GP2 */
  58. .config_regs = {0x00, 0x2c, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x32},
  59. .base = 20,
  60. },
  61. {
  62. .data_reg = 0x4d, /* GP3 */
  63. .config_regs = {0x33, 0x34, 0x35, 0x36, 0x37, 0x00, 0x39, 0x3a},
  64. .base = 30,
  65. },
  66. {
  67. .data_reg = 0x4e, /* GP4 */
  68. .config_regs = {0x3b, 0x00, 0x3d, 0x00, 0x6e, 0x6f, 0x72, 0x73},
  69. .base = 40,
  70. },
  71. {
  72. .data_reg = 0x4f, /* GP5 */
  73. .config_regs = {0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46},
  74. .base = 50,
  75. },
  76. {
  77. .data_reg = 0x50, /* GP6 */
  78. .config_regs = {0x47, 0x48, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59},
  79. .base = 60,
  80. },
  81. };
  82. static inline struct sch311x_gpio_block *
  83. to_sch311x_gpio_block(struct gpio_chip *chip)
  84. {
  85. return container_of(chip, struct sch311x_gpio_block, chip);
  86. }
  87. /*
  88. * Super-IO functions
  89. */
  90. static inline int sch311x_sio_enter(int sio_config_port)
  91. {
  92. /* Don't step on other drivers' I/O space by accident. */
  93. if (!request_muxed_region(sio_config_port, 2, DRV_NAME)) {
  94. pr_err(DRV_NAME "I/O address 0x%04x already in use\n",
  95. sio_config_port);
  96. return -EBUSY;
  97. }
  98. outb(SIO_CONFIG_KEY_ENTER, sio_config_port);
  99. return 0;
  100. }
  101. static inline void sch311x_sio_exit(int sio_config_port)
  102. {
  103. outb(SIO_CONFIG_KEY_EXIT, sio_config_port);
  104. release_region(sio_config_port, 2);
  105. }
  106. static inline int sch311x_sio_inb(int sio_config_port, int reg)
  107. {
  108. outb(reg, sio_config_port);
  109. return inb(sio_config_port + 1);
  110. }
  111. static inline void sch311x_sio_outb(int sio_config_port, int reg, int val)
  112. {
  113. outb(reg, sio_config_port);
  114. outb(val, sio_config_port + 1);
  115. }
  116. /*
  117. * GPIO functions
  118. */
  119. static int sch311x_gpio_request(struct gpio_chip *chip, unsigned offset)
  120. {
  121. struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
  122. if (block->config_regs[offset] == 0) /* GPIO is not available */
  123. return -ENODEV;
  124. if (!request_region(block->runtime_reg + block->config_regs[offset],
  125. 1, DRV_NAME)) {
  126. dev_err(chip->dev, "Failed to request region 0x%04x.\n",
  127. block->runtime_reg + block->config_regs[offset]);
  128. return -EBUSY;
  129. }
  130. return 0;
  131. }
  132. static void sch311x_gpio_free(struct gpio_chip *chip, unsigned offset)
  133. {
  134. struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
  135. if (block->config_regs[offset] == 0) /* GPIO is not available */
  136. return;
  137. release_region(block->runtime_reg + block->config_regs[offset], 1);
  138. }
  139. static int sch311x_gpio_get(struct gpio_chip *chip, unsigned offset)
  140. {
  141. struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
  142. unsigned char data;
  143. spin_lock(&block->lock);
  144. data = inb(block->runtime_reg + block->data_reg);
  145. spin_unlock(&block->lock);
  146. return !!(data & BIT(offset));
  147. }
  148. static void __sch311x_gpio_set(struct sch311x_gpio_block *block,
  149. unsigned offset, int value)
  150. {
  151. unsigned char data = inb(block->runtime_reg + block->data_reg);
  152. if (value)
  153. data |= BIT(offset);
  154. else
  155. data &= ~BIT(offset);
  156. outb(data, block->runtime_reg + block->data_reg);
  157. }
  158. static void sch311x_gpio_set(struct gpio_chip *chip, unsigned offset,
  159. int value)
  160. {
  161. struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
  162. spin_lock(&block->lock);
  163. __sch311x_gpio_set(block, offset, value);
  164. spin_unlock(&block->lock);
  165. }
  166. static int sch311x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  167. {
  168. struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
  169. spin_lock(&block->lock);
  170. outb(SCH311X_GPIO_CONF_IN, block->runtime_reg +
  171. block->config_regs[offset]);
  172. spin_unlock(&block->lock);
  173. return 0;
  174. }
  175. static int sch311x_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
  176. int value)
  177. {
  178. struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
  179. spin_lock(&block->lock);
  180. outb(SCH311X_GPIO_CONF_OUT, block->runtime_reg +
  181. block->config_regs[offset]);
  182. __sch311x_gpio_set(block, offset, value);
  183. spin_unlock(&block->lock);
  184. return 0;
  185. }
  186. static int sch311x_gpio_probe(struct platform_device *pdev)
  187. {
  188. struct sch311x_pdev_data *pdata = pdev->dev.platform_data;
  189. struct sch311x_gpio_priv *priv;
  190. struct sch311x_gpio_block *block;
  191. int err, i;
  192. /* we can register all GPIO data registers at once */
  193. if (!request_region(pdata->runtime_reg + GP1, 6, DRV_NAME)) {
  194. dev_err(&pdev->dev, "Failed to request region 0x%04x-0x%04x.\n",
  195. pdata->runtime_reg + GP1, pdata->runtime_reg + GP1 + 5);
  196. return -EBUSY;
  197. }
  198. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  199. if (!priv)
  200. return -ENOMEM;
  201. platform_set_drvdata(pdev, priv);
  202. for (i = 0; i < ARRAY_SIZE(priv->blocks); i++) {
  203. block = &priv->blocks[i];
  204. spin_lock_init(&block->lock);
  205. block->chip.label = DRV_NAME;
  206. block->chip.owner = THIS_MODULE;
  207. block->chip.request = sch311x_gpio_request;
  208. block->chip.free = sch311x_gpio_free;
  209. block->chip.direction_input = sch311x_gpio_direction_in;
  210. block->chip.direction_output = sch311x_gpio_direction_out;
  211. block->chip.get = sch311x_gpio_get;
  212. block->chip.set = sch311x_gpio_set;
  213. block->chip.ngpio = 8;
  214. block->chip.dev = &pdev->dev;
  215. block->chip.base = sch311x_gpio_blocks[i].base;
  216. block->config_regs = sch311x_gpio_blocks[i].config_regs;
  217. block->data_reg = sch311x_gpio_blocks[i].data_reg;
  218. block->runtime_reg = pdata->runtime_reg;
  219. err = gpiochip_add(&block->chip);
  220. if (err < 0) {
  221. dev_err(&pdev->dev,
  222. "Could not register gpiochip, %d\n", err);
  223. goto exit_err;
  224. }
  225. dev_info(&pdev->dev,
  226. "SMSC SCH311x GPIO block %d registered.\n", i);
  227. }
  228. return 0;
  229. exit_err:
  230. release_region(pdata->runtime_reg + GP1, 6);
  231. /* release already registered chips */
  232. for (--i; i >= 0; i--)
  233. gpiochip_remove(&priv->blocks[i].chip);
  234. return err;
  235. }
  236. static int sch311x_gpio_remove(struct platform_device *pdev)
  237. {
  238. struct sch311x_pdev_data *pdata = pdev->dev.platform_data;
  239. struct sch311x_gpio_priv *priv = platform_get_drvdata(pdev);
  240. int i;
  241. release_region(pdata->runtime_reg + GP1, 6);
  242. for (i = 0; i < ARRAY_SIZE(priv->blocks); i++) {
  243. gpiochip_remove(&priv->blocks[i].chip);
  244. dev_info(&pdev->dev,
  245. "SMSC SCH311x GPIO block %d unregistered.\n", i);
  246. }
  247. return 0;
  248. }
  249. static struct platform_driver sch311x_gpio_driver = {
  250. .driver.name = DRV_NAME,
  251. .driver.owner = THIS_MODULE,
  252. .probe = sch311x_gpio_probe,
  253. .remove = sch311x_gpio_remove,
  254. };
  255. /*
  256. * Init & exit routines
  257. */
  258. static int __init sch311x_detect(int sio_config_port, unsigned short *addr)
  259. {
  260. int err = 0, reg;
  261. unsigned short base_addr;
  262. unsigned char dev_id;
  263. err = sch311x_sio_enter(sio_config_port);
  264. if (err)
  265. return err;
  266. /* Check device ID. */
  267. reg = sch311x_sio_inb(sio_config_port, 0x20);
  268. switch (reg) {
  269. case 0x7c: /* SCH3112 */
  270. dev_id = 2;
  271. break;
  272. case 0x7d: /* SCH3114 */
  273. dev_id = 4;
  274. break;
  275. case 0x7f: /* SCH3116 */
  276. dev_id = 6;
  277. break;
  278. default:
  279. err = -ENODEV;
  280. goto exit;
  281. }
  282. /* Select logical device A (runtime registers) */
  283. sch311x_sio_outb(sio_config_port, 0x07, 0x0a);
  284. /* Check if Logical Device Register is currently active */
  285. if ((sch311x_sio_inb(sio_config_port, 0x30) & 0x01) == 0)
  286. pr_info("Seems that LDN 0x0a is not active...\n");
  287. /* Get the base address of the runtime registers */
  288. base_addr = (sch311x_sio_inb(sio_config_port, 0x60) << 8) |
  289. sch311x_sio_inb(sio_config_port, 0x61);
  290. if (!base_addr) {
  291. pr_err("Base address not set\n");
  292. err = -ENODEV;
  293. goto exit;
  294. }
  295. *addr = base_addr;
  296. pr_info("Found an SMSC SCH311%d chip at 0x%04x\n", dev_id, base_addr);
  297. exit:
  298. sch311x_sio_exit(sio_config_port);
  299. return err;
  300. }
  301. static int __init sch311x_gpio_pdev_add(const unsigned short addr)
  302. {
  303. struct sch311x_pdev_data pdata;
  304. int err;
  305. pdata.runtime_reg = addr;
  306. sch311x_gpio_pdev = platform_device_alloc(DRV_NAME, -1);
  307. if (!sch311x_gpio_pdev)
  308. return -ENOMEM;
  309. err = platform_device_add_data(sch311x_gpio_pdev,
  310. &pdata, sizeof(pdata));
  311. if (err) {
  312. pr_err(DRV_NAME "Platform data allocation failed\n");
  313. goto err;
  314. }
  315. err = platform_device_add(sch311x_gpio_pdev);
  316. if (err) {
  317. pr_err(DRV_NAME "Device addition failed\n");
  318. goto err;
  319. }
  320. return 0;
  321. err:
  322. platform_device_put(sch311x_gpio_pdev);
  323. return err;
  324. }
  325. static int __init sch311x_gpio_init(void)
  326. {
  327. int err, i;
  328. unsigned short addr = 0;
  329. for (i = 0; i < ARRAY_SIZE(sch311x_ioports); i++)
  330. if (sch311x_detect(sch311x_ioports[i], &addr) == 0)
  331. break;
  332. if (!addr)
  333. return -ENODEV;
  334. err = platform_driver_register(&sch311x_gpio_driver);
  335. if (err)
  336. return err;
  337. err = sch311x_gpio_pdev_add(addr);
  338. if (err)
  339. goto unreg_platform_driver;
  340. return 0;
  341. unreg_platform_driver:
  342. platform_driver_unregister(&sch311x_gpio_driver);
  343. return err;
  344. }
  345. static void __exit sch311x_gpio_exit(void)
  346. {
  347. platform_device_unregister(sch311x_gpio_pdev);
  348. platform_driver_unregister(&sch311x_gpio_driver);
  349. }
  350. module_init(sch311x_gpio_init);
  351. module_exit(sch311x_gpio_exit);
  352. MODULE_AUTHOR("Bruno Randolf <br1@einfach.org>");
  353. MODULE_DESCRIPTION("SMSC SCH311x GPIO Driver");
  354. MODULE_LICENSE("GPL");
  355. MODULE_ALIAS("platform:gpio-sch311x");