gpio-sch311x.c 10 KB

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