gpio-sch311x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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/driver.h>
  20. #include <linux/bitops.h>
  21. #include <linux/io.h>
  22. #define DRV_NAME "gpio-sch311x"
  23. #define SCH311X_GPIO_CONF_DIR BIT(0)
  24. #define SCH311X_GPIO_CONF_INVERT BIT(1)
  25. #define SCH311X_GPIO_CONF_OPEN_DRAIN BIT(7)
  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. /*
  83. * Super-IO functions
  84. */
  85. static inline int sch311x_sio_enter(int sio_config_port)
  86. {
  87. /* Don't step on other drivers' I/O space by accident. */
  88. if (!request_muxed_region(sio_config_port, 2, DRV_NAME)) {
  89. pr_err(DRV_NAME "I/O address 0x%04x already in use\n",
  90. sio_config_port);
  91. return -EBUSY;
  92. }
  93. outb(SIO_CONFIG_KEY_ENTER, sio_config_port);
  94. return 0;
  95. }
  96. static inline void sch311x_sio_exit(int sio_config_port)
  97. {
  98. outb(SIO_CONFIG_KEY_EXIT, sio_config_port);
  99. release_region(sio_config_port, 2);
  100. }
  101. static inline int sch311x_sio_inb(int sio_config_port, int reg)
  102. {
  103. outb(reg, sio_config_port);
  104. return inb(sio_config_port + 1);
  105. }
  106. static inline void sch311x_sio_outb(int sio_config_port, int reg, int val)
  107. {
  108. outb(reg, sio_config_port);
  109. outb(val, sio_config_port + 1);
  110. }
  111. /*
  112. * GPIO functions
  113. */
  114. static int sch311x_gpio_request(struct gpio_chip *chip, unsigned offset)
  115. {
  116. struct sch311x_gpio_block *block = gpiochip_get_data(chip);
  117. if (block->config_regs[offset] == 0) /* GPIO is not available */
  118. return -ENODEV;
  119. if (!request_region(block->runtime_reg + block->config_regs[offset],
  120. 1, DRV_NAME)) {
  121. dev_err(chip->parent, "Failed to request region 0x%04x.\n",
  122. block->runtime_reg + block->config_regs[offset]);
  123. return -EBUSY;
  124. }
  125. return 0;
  126. }
  127. static void sch311x_gpio_free(struct gpio_chip *chip, unsigned offset)
  128. {
  129. struct sch311x_gpio_block *block = gpiochip_get_data(chip);
  130. if (block->config_regs[offset] == 0) /* GPIO is not available */
  131. return;
  132. release_region(block->runtime_reg + block->config_regs[offset], 1);
  133. }
  134. static int sch311x_gpio_get(struct gpio_chip *chip, unsigned offset)
  135. {
  136. struct sch311x_gpio_block *block = gpiochip_get_data(chip);
  137. u8 data;
  138. spin_lock(&block->lock);
  139. data = inb(block->runtime_reg + block->data_reg);
  140. spin_unlock(&block->lock);
  141. return !!(data & BIT(offset));
  142. }
  143. static void __sch311x_gpio_set(struct sch311x_gpio_block *block,
  144. unsigned offset, int value)
  145. {
  146. u8 data = inb(block->runtime_reg + block->data_reg);
  147. if (value)
  148. data |= BIT(offset);
  149. else
  150. data &= ~BIT(offset);
  151. outb(data, block->runtime_reg + block->data_reg);
  152. }
  153. static void sch311x_gpio_set(struct gpio_chip *chip, unsigned offset,
  154. int value)
  155. {
  156. struct sch311x_gpio_block *block = gpiochip_get_data(chip);
  157. spin_lock(&block->lock);
  158. __sch311x_gpio_set(block, offset, value);
  159. spin_unlock(&block->lock);
  160. }
  161. static int sch311x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  162. {
  163. struct sch311x_gpio_block *block = gpiochip_get_data(chip);
  164. u8 data;
  165. spin_lock(&block->lock);
  166. data = inb(block->runtime_reg + block->config_regs[offset]);
  167. data |= SCH311X_GPIO_CONF_DIR;
  168. outb(data, block->runtime_reg + block->config_regs[offset]);
  169. spin_unlock(&block->lock);
  170. return 0;
  171. }
  172. static int sch311x_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
  173. int value)
  174. {
  175. struct sch311x_gpio_block *block = gpiochip_get_data(chip);
  176. u8 data;
  177. spin_lock(&block->lock);
  178. data = inb(block->runtime_reg + block->config_regs[offset]);
  179. data &= ~SCH311X_GPIO_CONF_DIR;
  180. outb(data, block->runtime_reg + block->config_regs[offset]);
  181. __sch311x_gpio_set(block, offset, value);
  182. spin_unlock(&block->lock);
  183. return 0;
  184. }
  185. static int sch311x_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  186. {
  187. struct sch311x_gpio_block *block = gpiochip_get_data(chip);
  188. u8 data;
  189. spin_lock(&block->lock);
  190. data = inb(block->runtime_reg + block->config_regs[offset]);
  191. spin_unlock(&block->lock);
  192. return !!(data & SCH311X_GPIO_CONF_DIR);
  193. }
  194. static int sch311x_gpio_set_config(struct gpio_chip *chip, unsigned offset,
  195. unsigned long config)
  196. {
  197. struct sch311x_gpio_block *block = gpiochip_get_data(chip);
  198. enum pin_config_param param = pinconf_to_config_param(config);
  199. u8 data;
  200. switch (param) {
  201. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  202. spin_lock(&block->lock);
  203. data = inb(block->runtime_reg + block->config_regs[offset]);
  204. data |= SCH311X_GPIO_CONF_OPEN_DRAIN;
  205. outb(data, block->runtime_reg + block->config_regs[offset]);
  206. spin_unlock(&block->lock);
  207. return 0;
  208. case PIN_CONFIG_DRIVE_PUSH_PULL:
  209. spin_lock(&block->lock);
  210. data = inb(block->runtime_reg + block->config_regs[offset]);
  211. data &= ~SCH311X_GPIO_CONF_OPEN_DRAIN;
  212. outb(data, block->runtime_reg + block->config_regs[offset]);
  213. spin_unlock(&block->lock);
  214. return 0;
  215. default:
  216. break;
  217. }
  218. return -ENOTSUPP;
  219. }
  220. static int sch311x_gpio_probe(struct platform_device *pdev)
  221. {
  222. struct sch311x_pdev_data *pdata = dev_get_platdata(&pdev->dev);
  223. struct sch311x_gpio_priv *priv;
  224. struct sch311x_gpio_block *block;
  225. int err, i;
  226. /* we can register all GPIO data registers at once */
  227. if (!devm_request_region(&pdev->dev, pdata->runtime_reg + GP1, 6,
  228. DRV_NAME)) {
  229. dev_err(&pdev->dev, "Failed to request region 0x%04x-0x%04x.\n",
  230. pdata->runtime_reg + GP1, pdata->runtime_reg + GP1 + 5);
  231. return -EBUSY;
  232. }
  233. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  234. if (!priv)
  235. return -ENOMEM;
  236. platform_set_drvdata(pdev, priv);
  237. for (i = 0; i < ARRAY_SIZE(priv->blocks); i++) {
  238. block = &priv->blocks[i];
  239. spin_lock_init(&block->lock);
  240. block->chip.label = DRV_NAME;
  241. block->chip.owner = THIS_MODULE;
  242. block->chip.request = sch311x_gpio_request;
  243. block->chip.free = sch311x_gpio_free;
  244. block->chip.direction_input = sch311x_gpio_direction_in;
  245. block->chip.direction_output = sch311x_gpio_direction_out;
  246. block->chip.get_direction = sch311x_gpio_get_direction;
  247. block->chip.set_config = sch311x_gpio_set_config;
  248. block->chip.get = sch311x_gpio_get;
  249. block->chip.set = sch311x_gpio_set;
  250. block->chip.ngpio = 8;
  251. block->chip.parent = &pdev->dev;
  252. block->chip.base = sch311x_gpio_blocks[i].base;
  253. block->config_regs = sch311x_gpio_blocks[i].config_regs;
  254. block->data_reg = sch311x_gpio_blocks[i].data_reg;
  255. block->runtime_reg = pdata->runtime_reg;
  256. err = gpiochip_add_data(&block->chip, block);
  257. if (err < 0) {
  258. dev_err(&pdev->dev,
  259. "Could not register gpiochip, %d\n", err);
  260. goto exit_err;
  261. }
  262. dev_info(&pdev->dev,
  263. "SMSC SCH311x GPIO block %d registered.\n", i);
  264. }
  265. return 0;
  266. exit_err:
  267. /* release already registered chips */
  268. for (--i; i >= 0; i--)
  269. gpiochip_remove(&priv->blocks[i].chip);
  270. return err;
  271. }
  272. static int sch311x_gpio_remove(struct platform_device *pdev)
  273. {
  274. struct sch311x_gpio_priv *priv = platform_get_drvdata(pdev);
  275. int i;
  276. for (i = 0; i < ARRAY_SIZE(priv->blocks); i++) {
  277. gpiochip_remove(&priv->blocks[i].chip);
  278. dev_info(&pdev->dev,
  279. "SMSC SCH311x GPIO block %d unregistered.\n", i);
  280. }
  281. return 0;
  282. }
  283. static struct platform_driver sch311x_gpio_driver = {
  284. .driver.name = DRV_NAME,
  285. .probe = sch311x_gpio_probe,
  286. .remove = sch311x_gpio_remove,
  287. };
  288. /*
  289. * Init & exit routines
  290. */
  291. static int __init sch311x_detect(int sio_config_port, unsigned short *addr)
  292. {
  293. int err = 0, reg;
  294. unsigned short base_addr;
  295. u8 dev_id;
  296. err = sch311x_sio_enter(sio_config_port);
  297. if (err)
  298. return err;
  299. /* Check device ID. */
  300. reg = sch311x_sio_inb(sio_config_port, 0x20);
  301. switch (reg) {
  302. case 0x7c: /* SCH3112 */
  303. dev_id = 2;
  304. break;
  305. case 0x7d: /* SCH3114 */
  306. dev_id = 4;
  307. break;
  308. case 0x7f: /* SCH3116 */
  309. dev_id = 6;
  310. break;
  311. default:
  312. err = -ENODEV;
  313. goto exit;
  314. }
  315. /* Select logical device A (runtime registers) */
  316. sch311x_sio_outb(sio_config_port, 0x07, 0x0a);
  317. /* Check if Logical Device Register is currently active */
  318. if ((sch311x_sio_inb(sio_config_port, 0x30) & 0x01) == 0)
  319. pr_info("Seems that LDN 0x0a is not active...\n");
  320. /* Get the base address of the runtime registers */
  321. base_addr = (sch311x_sio_inb(sio_config_port, 0x60) << 8) |
  322. sch311x_sio_inb(sio_config_port, 0x61);
  323. if (!base_addr) {
  324. pr_err("Base address not set\n");
  325. err = -ENODEV;
  326. goto exit;
  327. }
  328. *addr = base_addr;
  329. pr_info("Found an SMSC SCH311%d chip at 0x%04x\n", dev_id, base_addr);
  330. exit:
  331. sch311x_sio_exit(sio_config_port);
  332. return err;
  333. }
  334. static int __init sch311x_gpio_pdev_add(const unsigned short addr)
  335. {
  336. struct sch311x_pdev_data pdata;
  337. int err;
  338. pdata.runtime_reg = addr;
  339. sch311x_gpio_pdev = platform_device_alloc(DRV_NAME, -1);
  340. if (!sch311x_gpio_pdev)
  341. return -ENOMEM;
  342. err = platform_device_add_data(sch311x_gpio_pdev,
  343. &pdata, sizeof(pdata));
  344. if (err) {
  345. pr_err(DRV_NAME "Platform data allocation failed\n");
  346. goto err;
  347. }
  348. err = platform_device_add(sch311x_gpio_pdev);
  349. if (err) {
  350. pr_err(DRV_NAME "Device addition failed\n");
  351. goto err;
  352. }
  353. return 0;
  354. err:
  355. platform_device_put(sch311x_gpio_pdev);
  356. return err;
  357. }
  358. static int __init sch311x_gpio_init(void)
  359. {
  360. int err, i;
  361. unsigned short addr = 0;
  362. for (i = 0; i < ARRAY_SIZE(sch311x_ioports); i++)
  363. if (sch311x_detect(sch311x_ioports[i], &addr) == 0)
  364. break;
  365. if (!addr)
  366. return -ENODEV;
  367. err = platform_driver_register(&sch311x_gpio_driver);
  368. if (err)
  369. return err;
  370. err = sch311x_gpio_pdev_add(addr);
  371. if (err)
  372. goto unreg_platform_driver;
  373. return 0;
  374. unreg_platform_driver:
  375. platform_driver_unregister(&sch311x_gpio_driver);
  376. return err;
  377. }
  378. static void __exit sch311x_gpio_exit(void)
  379. {
  380. platform_device_unregister(sch311x_gpio_pdev);
  381. platform_driver_unregister(&sch311x_gpio_driver);
  382. }
  383. module_init(sch311x_gpio_init);
  384. module_exit(sch311x_gpio_exit);
  385. MODULE_AUTHOR("Bruno Randolf <br1@einfach.org>");
  386. MODULE_DESCRIPTION("SMSC SCH311x GPIO Driver");
  387. MODULE_LICENSE("GPL");
  388. MODULE_ALIAS("platform:gpio-sch311x");