akita-ioexp.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Support for the Extra GPIOs on the Sharp SL-C1000 (Akita)
  3. * (uses a Maxim MAX7310 8 Port IO Expander)
  4. *
  5. * Copyright 2005 Openedhand Ltd.
  6. *
  7. * Author: Richard Purdie <richard@openedhand.com>
  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 version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/module.h>
  18. #include <linux/i2c.h>
  19. #include <linux/slab.h>
  20. #include <linux/workqueue.h>
  21. #include <asm/arch/akita.h>
  22. /* MAX7310 Regiser Map */
  23. #define MAX7310_INPUT 0x00
  24. #define MAX7310_OUTPUT 0x01
  25. #define MAX7310_POLINV 0x02
  26. #define MAX7310_IODIR 0x03 /* 1 = Input, 0 = Output */
  27. #define MAX7310_TIMEOUT 0x04
  28. /* Addresses to scan */
  29. static unsigned short normal_i2c[] = { 0x18, I2C_CLIENT_END };
  30. /* I2C Magic */
  31. I2C_CLIENT_INSMOD;
  32. static int max7310_write(struct i2c_client *client, int address, int data);
  33. static struct i2c_client max7310_template;
  34. static void akita_ioexp_work(void *private_);
  35. static struct device *akita_ioexp_device;
  36. static unsigned char ioexp_output_value = AKITA_IOEXP_IO_OUT;
  37. DECLARE_WORK(akita_ioexp, akita_ioexp_work, NULL);
  38. /*
  39. * MAX7310 Access
  40. */
  41. static int max7310_config(struct device *dev, int iomode, int polarity)
  42. {
  43. int ret;
  44. struct i2c_client *client = to_i2c_client(dev);
  45. ret = max7310_write(client, MAX7310_POLINV, polarity);
  46. if (ret < 0)
  47. return ret;
  48. ret = max7310_write(client, MAX7310_IODIR, iomode);
  49. return ret;
  50. }
  51. static int max7310_set_ouputs(struct device *dev, int outputs)
  52. {
  53. struct i2c_client *client = to_i2c_client(dev);
  54. return max7310_write(client, MAX7310_OUTPUT, outputs);
  55. }
  56. /*
  57. * I2C Functions
  58. */
  59. static int max7310_write(struct i2c_client *client, int address, int value)
  60. {
  61. u8 data[2];
  62. data[0] = address & 0xff;
  63. data[1] = value & 0xff;
  64. if (i2c_master_send(client, data, 2) == 2)
  65. return 0;
  66. return -1;
  67. }
  68. static int max7310_detect(struct i2c_adapter *adapter, int address, int kind)
  69. {
  70. struct i2c_client *new_client;
  71. int err;
  72. if (!(new_client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL)))
  73. return -ENOMEM;
  74. max7310_template.adapter = adapter;
  75. max7310_template.addr = address;
  76. memcpy(new_client, &max7310_template, sizeof(struct i2c_client));
  77. if ((err = i2c_attach_client(new_client))) {
  78. kfree(new_client);
  79. return err;
  80. }
  81. max7310_config(&new_client->dev, AKITA_IOEXP_IO_DIR, 0);
  82. akita_ioexp_device = &new_client->dev;
  83. schedule_work(&akita_ioexp);
  84. return 0;
  85. }
  86. static int max7310_attach_adapter(struct i2c_adapter *adapter)
  87. {
  88. return i2c_probe(adapter, &addr_data, max7310_detect);
  89. }
  90. static int max7310_detach_client(struct i2c_client *client)
  91. {
  92. int err;
  93. akita_ioexp_device = NULL;
  94. if ((err = i2c_detach_client(client)))
  95. return err;
  96. kfree(client);
  97. return 0;
  98. }
  99. static struct i2c_driver max7310_i2c_driver = {
  100. .owner = THIS_MODULE,
  101. .name = "akita-max7310",
  102. .id = I2C_DRIVERID_AKITAIOEXP,
  103. .flags = I2C_DF_NOTIFY,
  104. .attach_adapter = max7310_attach_adapter,
  105. .detach_client = max7310_detach_client,
  106. };
  107. static struct i2c_client max7310_template = {
  108. name: "akita-max7310",
  109. flags: I2C_CLIENT_ALLOW_USE,
  110. driver: &max7310_i2c_driver,
  111. };
  112. void akita_set_ioexp(struct device *dev, unsigned char bit)
  113. {
  114. ioexp_output_value |= bit;
  115. if (akita_ioexp_device)
  116. schedule_work(&akita_ioexp);
  117. return;
  118. }
  119. void akita_reset_ioexp(struct device *dev, unsigned char bit)
  120. {
  121. ioexp_output_value &= ~bit;
  122. if (akita_ioexp_device)
  123. schedule_work(&akita_ioexp);
  124. return;
  125. }
  126. EXPORT_SYMBOL(akita_set_ioexp);
  127. EXPORT_SYMBOL(akita_reset_ioexp);
  128. static void akita_ioexp_work(void *private_)
  129. {
  130. if (akita_ioexp_device)
  131. max7310_set_ouputs(akita_ioexp_device, ioexp_output_value);
  132. }
  133. #ifdef CONFIG_PM
  134. static int akita_ioexp_suspend(struct platform_device *pdev, pm_message_t state)
  135. {
  136. flush_scheduled_work();
  137. return 0;
  138. }
  139. static int akita_ioexp_resume(struct platform_device *pdev)
  140. {
  141. schedule_work(&akita_ioexp);
  142. return 0;
  143. }
  144. #else
  145. #define akita_ioexp_suspend NULL
  146. #define akita_ioexp_resume NULL
  147. #endif
  148. static int __init akita_ioexp_probe(struct platform_device *pdev)
  149. {
  150. return i2c_add_driver(&max7310_i2c_driver);
  151. }
  152. static int akita_ioexp_remove(struct platform_device *pdev)
  153. {
  154. i2c_del_driver(&max7310_i2c_driver);
  155. return 0;
  156. }
  157. static struct platform_driver akita_ioexp_driver = {
  158. .probe = akita_ioexp_probe,
  159. .remove = akita_ioexp_remove,
  160. .suspend = akita_ioexp_suspend,
  161. .resume = akita_ioexp_resume,
  162. .driver = {
  163. .name = "akita-ioexp",
  164. },
  165. };
  166. static int __init akita_ioexp_init(void)
  167. {
  168. return platform_driver_register(&akita_ioexp_driver);
  169. }
  170. static void __exit akita_ioexp_exit(void)
  171. {
  172. platform_driver_unregister(&akita_ioexp_driver);
  173. }
  174. MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
  175. MODULE_DESCRIPTION("Akita IO-Expander driver");
  176. MODULE_LICENSE("GPL");
  177. fs_initcall(akita_ioexp_init);
  178. module_exit(akita_ioexp_exit);