gpio-f7188x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * GPIO driver for Fintek Super-I/O F71869, F71869A, F71882, F71889 and F81866
  3. *
  4. * Copyright (C) 2010-2013 LaCie
  5. *
  6. * Author: Simon Guinot <simon.guinot@sequanux.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/io.h>
  17. #include <linux/gpio/driver.h>
  18. #include <linux/bitops.h>
  19. #define DRVNAME "gpio-f7188x"
  20. /*
  21. * Super-I/O registers
  22. */
  23. #define SIO_LDSEL 0x07 /* Logical device select */
  24. #define SIO_DEVID 0x20 /* Device ID (2 bytes) */
  25. #define SIO_DEVREV 0x22 /* Device revision */
  26. #define SIO_MANID 0x23 /* Fintek ID (2 bytes) */
  27. #define SIO_LD_GPIO 0x06 /* GPIO logical device */
  28. #define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */
  29. #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
  30. #define SIO_FINTEK_ID 0x1934 /* Manufacturer ID */
  31. #define SIO_F71869_ID 0x0814 /* F71869 chipset ID */
  32. #define SIO_F71869A_ID 0x1007 /* F71869A chipset ID */
  33. #define SIO_F71882_ID 0x0541 /* F71882 chipset ID */
  34. #define SIO_F71889_ID 0x0909 /* F71889 chipset ID */
  35. #define SIO_F81866_ID 0x1010 /* F81866 chipset ID */
  36. enum chips { f71869, f71869a, f71882fg, f71889f, f81866 };
  37. static const char * const f7188x_names[] = {
  38. "f71869",
  39. "f71869a",
  40. "f71882fg",
  41. "f71889f",
  42. "f81866",
  43. };
  44. struct f7188x_sio {
  45. int addr;
  46. enum chips type;
  47. };
  48. struct f7188x_gpio_bank {
  49. struct gpio_chip chip;
  50. unsigned int regbase;
  51. struct f7188x_gpio_data *data;
  52. };
  53. struct f7188x_gpio_data {
  54. struct f7188x_sio *sio;
  55. int nr_bank;
  56. struct f7188x_gpio_bank *bank;
  57. };
  58. /*
  59. * Super-I/O functions.
  60. */
  61. static inline int superio_inb(int base, int reg)
  62. {
  63. outb(reg, base);
  64. return inb(base + 1);
  65. }
  66. static int superio_inw(int base, int reg)
  67. {
  68. int val;
  69. outb(reg++, base);
  70. val = inb(base + 1) << 8;
  71. outb(reg, base);
  72. val |= inb(base + 1);
  73. return val;
  74. }
  75. static inline void superio_outb(int base, int reg, int val)
  76. {
  77. outb(reg, base);
  78. outb(val, base + 1);
  79. }
  80. static inline int superio_enter(int base)
  81. {
  82. /* Don't step on other drivers' I/O space by accident. */
  83. if (!request_muxed_region(base, 2, DRVNAME)) {
  84. pr_err(DRVNAME "I/O address 0x%04x already in use\n", base);
  85. return -EBUSY;
  86. }
  87. /* According to the datasheet the key must be send twice. */
  88. outb(SIO_UNLOCK_KEY, base);
  89. outb(SIO_UNLOCK_KEY, base);
  90. return 0;
  91. }
  92. static inline void superio_select(int base, int ld)
  93. {
  94. outb(SIO_LDSEL, base);
  95. outb(ld, base + 1);
  96. }
  97. static inline void superio_exit(int base)
  98. {
  99. outb(SIO_LOCK_KEY, base);
  100. release_region(base, 2);
  101. }
  102. /*
  103. * GPIO chip.
  104. */
  105. static int f7188x_gpio_get_direction(struct gpio_chip *chip, unsigned offset);
  106. static int f7188x_gpio_direction_in(struct gpio_chip *chip, unsigned offset);
  107. static int f7188x_gpio_get(struct gpio_chip *chip, unsigned offset);
  108. static int f7188x_gpio_direction_out(struct gpio_chip *chip,
  109. unsigned offset, int value);
  110. static void f7188x_gpio_set(struct gpio_chip *chip, unsigned offset, int value);
  111. static int f7188x_gpio_set_single_ended(struct gpio_chip *gc,
  112. unsigned offset,
  113. enum single_ended_mode mode);
  114. #define F7188X_GPIO_BANK(_base, _ngpio, _regbase) \
  115. { \
  116. .chip = { \
  117. .label = DRVNAME, \
  118. .owner = THIS_MODULE, \
  119. .get_direction = f7188x_gpio_get_direction, \
  120. .direction_input = f7188x_gpio_direction_in, \
  121. .get = f7188x_gpio_get, \
  122. .direction_output = f7188x_gpio_direction_out, \
  123. .set = f7188x_gpio_set, \
  124. .set_single_ended = f7188x_gpio_set_single_ended, \
  125. .base = _base, \
  126. .ngpio = _ngpio, \
  127. .can_sleep = true, \
  128. }, \
  129. .regbase = _regbase, \
  130. }
  131. #define gpio_dir(base) (base + 0)
  132. #define gpio_data_out(base) (base + 1)
  133. #define gpio_data_in(base) (base + 2)
  134. /* Output mode register (0:open drain 1:push-pull). */
  135. #define gpio_out_mode(base) (base + 3)
  136. static struct f7188x_gpio_bank f71869_gpio_bank[] = {
  137. F7188X_GPIO_BANK(0, 6, 0xF0),
  138. F7188X_GPIO_BANK(10, 8, 0xE0),
  139. F7188X_GPIO_BANK(20, 8, 0xD0),
  140. F7188X_GPIO_BANK(30, 8, 0xC0),
  141. F7188X_GPIO_BANK(40, 8, 0xB0),
  142. F7188X_GPIO_BANK(50, 5, 0xA0),
  143. F7188X_GPIO_BANK(60, 6, 0x90),
  144. };
  145. static struct f7188x_gpio_bank f71869a_gpio_bank[] = {
  146. F7188X_GPIO_BANK(0, 6, 0xF0),
  147. F7188X_GPIO_BANK(10, 8, 0xE0),
  148. F7188X_GPIO_BANK(20, 8, 0xD0),
  149. F7188X_GPIO_BANK(30, 8, 0xC0),
  150. F7188X_GPIO_BANK(40, 8, 0xB0),
  151. F7188X_GPIO_BANK(50, 5, 0xA0),
  152. F7188X_GPIO_BANK(60, 8, 0x90),
  153. F7188X_GPIO_BANK(70, 8, 0x80),
  154. };
  155. static struct f7188x_gpio_bank f71882_gpio_bank[] = {
  156. F7188X_GPIO_BANK(0, 8, 0xF0),
  157. F7188X_GPIO_BANK(10, 8, 0xE0),
  158. F7188X_GPIO_BANK(20, 8, 0xD0),
  159. F7188X_GPIO_BANK(30, 4, 0xC0),
  160. F7188X_GPIO_BANK(40, 4, 0xB0),
  161. };
  162. static struct f7188x_gpio_bank f71889_gpio_bank[] = {
  163. F7188X_GPIO_BANK(0, 7, 0xF0),
  164. F7188X_GPIO_BANK(10, 7, 0xE0),
  165. F7188X_GPIO_BANK(20, 8, 0xD0),
  166. F7188X_GPIO_BANK(30, 8, 0xC0),
  167. F7188X_GPIO_BANK(40, 8, 0xB0),
  168. F7188X_GPIO_BANK(50, 5, 0xA0),
  169. F7188X_GPIO_BANK(60, 8, 0x90),
  170. F7188X_GPIO_BANK(70, 8, 0x80),
  171. };
  172. static struct f7188x_gpio_bank f81866_gpio_bank[] = {
  173. F7188X_GPIO_BANK(0, 8, 0xF0),
  174. F7188X_GPIO_BANK(10, 8, 0xE0),
  175. F7188X_GPIO_BANK(20, 8, 0xD0),
  176. F7188X_GPIO_BANK(30, 8, 0xC0),
  177. F7188X_GPIO_BANK(40, 8, 0xB0),
  178. F7188X_GPIO_BANK(50, 8, 0xA0),
  179. F7188X_GPIO_BANK(60, 8, 0x90),
  180. F7188X_GPIO_BANK(70, 8, 0x80),
  181. F7188X_GPIO_BANK(80, 8, 0x88),
  182. };
  183. static int f7188x_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  184. {
  185. int err;
  186. struct f7188x_gpio_bank *bank =
  187. container_of(chip, struct f7188x_gpio_bank, chip);
  188. struct f7188x_sio *sio = bank->data->sio;
  189. u8 dir;
  190. err = superio_enter(sio->addr);
  191. if (err)
  192. return err;
  193. superio_select(sio->addr, SIO_LD_GPIO);
  194. dir = superio_inb(sio->addr, gpio_dir(bank->regbase));
  195. superio_exit(sio->addr);
  196. return !(dir & 1 << offset);
  197. }
  198. static int f7188x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  199. {
  200. int err;
  201. struct f7188x_gpio_bank *bank = gpiochip_get_data(chip);
  202. struct f7188x_sio *sio = bank->data->sio;
  203. u8 dir;
  204. err = superio_enter(sio->addr);
  205. if (err)
  206. return err;
  207. superio_select(sio->addr, SIO_LD_GPIO);
  208. dir = superio_inb(sio->addr, gpio_dir(bank->regbase));
  209. dir &= ~BIT(offset);
  210. superio_outb(sio->addr, gpio_dir(bank->regbase), dir);
  211. superio_exit(sio->addr);
  212. return 0;
  213. }
  214. static int f7188x_gpio_get(struct gpio_chip *chip, unsigned offset)
  215. {
  216. int err;
  217. struct f7188x_gpio_bank *bank = gpiochip_get_data(chip);
  218. struct f7188x_sio *sio = bank->data->sio;
  219. u8 dir, data;
  220. err = superio_enter(sio->addr);
  221. if (err)
  222. return err;
  223. superio_select(sio->addr, SIO_LD_GPIO);
  224. dir = superio_inb(sio->addr, gpio_dir(bank->regbase));
  225. dir = !!(dir & BIT(offset));
  226. if (dir)
  227. data = superio_inb(sio->addr, gpio_data_out(bank->regbase));
  228. else
  229. data = superio_inb(sio->addr, gpio_data_in(bank->regbase));
  230. superio_exit(sio->addr);
  231. return !!(data & BIT(offset));
  232. }
  233. static int f7188x_gpio_direction_out(struct gpio_chip *chip,
  234. unsigned offset, int value)
  235. {
  236. int err;
  237. struct f7188x_gpio_bank *bank = gpiochip_get_data(chip);
  238. struct f7188x_sio *sio = bank->data->sio;
  239. u8 dir, data_out;
  240. err = superio_enter(sio->addr);
  241. if (err)
  242. return err;
  243. superio_select(sio->addr, SIO_LD_GPIO);
  244. data_out = superio_inb(sio->addr, gpio_data_out(bank->regbase));
  245. if (value)
  246. data_out |= BIT(offset);
  247. else
  248. data_out &= ~BIT(offset);
  249. superio_outb(sio->addr, gpio_data_out(bank->regbase), data_out);
  250. dir = superio_inb(sio->addr, gpio_dir(bank->regbase));
  251. dir |= BIT(offset);
  252. superio_outb(sio->addr, gpio_dir(bank->regbase), dir);
  253. superio_exit(sio->addr);
  254. return 0;
  255. }
  256. static void f7188x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  257. {
  258. int err;
  259. struct f7188x_gpio_bank *bank = gpiochip_get_data(chip);
  260. struct f7188x_sio *sio = bank->data->sio;
  261. u8 data_out;
  262. err = superio_enter(sio->addr);
  263. if (err)
  264. return;
  265. superio_select(sio->addr, SIO_LD_GPIO);
  266. data_out = superio_inb(sio->addr, gpio_data_out(bank->regbase));
  267. if (value)
  268. data_out |= BIT(offset);
  269. else
  270. data_out &= ~BIT(offset);
  271. superio_outb(sio->addr, gpio_data_out(bank->regbase), data_out);
  272. superio_exit(sio->addr);
  273. }
  274. static int f7188x_gpio_set_single_ended(struct gpio_chip *chip,
  275. unsigned offset,
  276. enum single_ended_mode mode)
  277. {
  278. int err;
  279. struct f7188x_gpio_bank *bank = gpiochip_get_data(chip);
  280. struct f7188x_sio *sio = bank->data->sio;
  281. u8 data;
  282. if (mode != LINE_MODE_OPEN_DRAIN &&
  283. mode != LINE_MODE_PUSH_PULL)
  284. return -ENOTSUPP;
  285. err = superio_enter(sio->addr);
  286. if (err)
  287. return err;
  288. superio_select(sio->addr, SIO_LD_GPIO);
  289. data = superio_inb(sio->addr, gpio_out_mode(bank->regbase));
  290. if (mode == LINE_MODE_OPEN_DRAIN)
  291. data &= ~BIT(offset);
  292. else
  293. data |= BIT(offset);
  294. superio_outb(sio->addr, gpio_out_mode(bank->regbase), data);
  295. superio_exit(sio->addr);
  296. return 0;
  297. }
  298. /*
  299. * Platform device and driver.
  300. */
  301. static int f7188x_gpio_probe(struct platform_device *pdev)
  302. {
  303. int err;
  304. int i;
  305. struct f7188x_sio *sio = dev_get_platdata(&pdev->dev);
  306. struct f7188x_gpio_data *data;
  307. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  308. if (!data)
  309. return -ENOMEM;
  310. switch (sio->type) {
  311. case f71869:
  312. data->nr_bank = ARRAY_SIZE(f71869_gpio_bank);
  313. data->bank = f71869_gpio_bank;
  314. break;
  315. case f71869a:
  316. data->nr_bank = ARRAY_SIZE(f71869a_gpio_bank);
  317. data->bank = f71869a_gpio_bank;
  318. break;
  319. case f71882fg:
  320. data->nr_bank = ARRAY_SIZE(f71882_gpio_bank);
  321. data->bank = f71882_gpio_bank;
  322. break;
  323. case f71889f:
  324. data->nr_bank = ARRAY_SIZE(f71889_gpio_bank);
  325. data->bank = f71889_gpio_bank;
  326. break;
  327. case f81866:
  328. data->nr_bank = ARRAY_SIZE(f81866_gpio_bank);
  329. data->bank = f81866_gpio_bank;
  330. break;
  331. default:
  332. return -ENODEV;
  333. }
  334. data->sio = sio;
  335. platform_set_drvdata(pdev, data);
  336. /* For each GPIO bank, register a GPIO chip. */
  337. for (i = 0; i < data->nr_bank; i++) {
  338. struct f7188x_gpio_bank *bank = &data->bank[i];
  339. bank->chip.parent = &pdev->dev;
  340. bank->data = data;
  341. err = devm_gpiochip_add_data(&pdev->dev, &bank->chip, bank);
  342. if (err) {
  343. dev_err(&pdev->dev,
  344. "Failed to register gpiochip %d: %d\n",
  345. i, err);
  346. return err;
  347. }
  348. }
  349. return 0;
  350. }
  351. static int __init f7188x_find(int addr, struct f7188x_sio *sio)
  352. {
  353. int err;
  354. u16 devid;
  355. err = superio_enter(addr);
  356. if (err)
  357. return err;
  358. err = -ENODEV;
  359. devid = superio_inw(addr, SIO_MANID);
  360. if (devid != SIO_FINTEK_ID) {
  361. pr_debug(DRVNAME ": Not a Fintek device at 0x%08x\n", addr);
  362. goto err;
  363. }
  364. devid = superio_inw(addr, SIO_DEVID);
  365. switch (devid) {
  366. case SIO_F71869_ID:
  367. sio->type = f71869;
  368. break;
  369. case SIO_F71869A_ID:
  370. sio->type = f71869a;
  371. break;
  372. case SIO_F71882_ID:
  373. sio->type = f71882fg;
  374. break;
  375. case SIO_F71889_ID:
  376. sio->type = f71889f;
  377. break;
  378. case SIO_F81866_ID:
  379. sio->type = f81866;
  380. break;
  381. default:
  382. pr_info(DRVNAME ": Unsupported Fintek device 0x%04x\n", devid);
  383. goto err;
  384. }
  385. sio->addr = addr;
  386. err = 0;
  387. pr_info(DRVNAME ": Found %s at %#x, revision %d\n",
  388. f7188x_names[sio->type],
  389. (unsigned int) addr,
  390. (int) superio_inb(addr, SIO_DEVREV));
  391. err:
  392. superio_exit(addr);
  393. return err;
  394. }
  395. static struct platform_device *f7188x_gpio_pdev;
  396. static int __init
  397. f7188x_gpio_device_add(const struct f7188x_sio *sio)
  398. {
  399. int err;
  400. f7188x_gpio_pdev = platform_device_alloc(DRVNAME, -1);
  401. if (!f7188x_gpio_pdev)
  402. return -ENOMEM;
  403. err = platform_device_add_data(f7188x_gpio_pdev,
  404. sio, sizeof(*sio));
  405. if (err) {
  406. pr_err(DRVNAME "Platform data allocation failed\n");
  407. goto err;
  408. }
  409. err = platform_device_add(f7188x_gpio_pdev);
  410. if (err) {
  411. pr_err(DRVNAME "Device addition failed\n");
  412. goto err;
  413. }
  414. return 0;
  415. err:
  416. platform_device_put(f7188x_gpio_pdev);
  417. return err;
  418. }
  419. /*
  420. * Try to match a supported Fintek device by reading the (hard-wired)
  421. * configuration I/O ports. If available, then register both the platform
  422. * device and driver to support the GPIOs.
  423. */
  424. static struct platform_driver f7188x_gpio_driver = {
  425. .driver = {
  426. .name = DRVNAME,
  427. },
  428. .probe = f7188x_gpio_probe,
  429. };
  430. static int __init f7188x_gpio_init(void)
  431. {
  432. int err;
  433. struct f7188x_sio sio;
  434. if (f7188x_find(0x2e, &sio) &&
  435. f7188x_find(0x4e, &sio))
  436. return -ENODEV;
  437. err = platform_driver_register(&f7188x_gpio_driver);
  438. if (!err) {
  439. err = f7188x_gpio_device_add(&sio);
  440. if (err)
  441. platform_driver_unregister(&f7188x_gpio_driver);
  442. }
  443. return err;
  444. }
  445. subsys_initcall(f7188x_gpio_init);
  446. static void __exit f7188x_gpio_exit(void)
  447. {
  448. platform_device_unregister(f7188x_gpio_pdev);
  449. platform_driver_unregister(&f7188x_gpio_driver);
  450. }
  451. module_exit(f7188x_gpio_exit);
  452. MODULE_DESCRIPTION("GPIO driver for Super-I/O chips F71869, F71869A, F71882FG, F71889F and F81866");
  453. MODULE_AUTHOR("Simon Guinot <simon.guinot@sequanux.org>");
  454. MODULE_LICENSE("GPL");