gpio-f7188x.c 13 KB

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