akita-ioexp.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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(struct work_struct *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);
  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. .driver = {
  101. .name = "akita-max7310",
  102. },
  103. .id = I2C_DRIVERID_AKITAIOEXP,
  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. driver: &max7310_i2c_driver,
  110. };
  111. void akita_set_ioexp(struct device *dev, unsigned char bit)
  112. {
  113. ioexp_output_value |= bit;
  114. if (akita_ioexp_device)
  115. schedule_work(&akita_ioexp);
  116. return;
  117. }
  118. void akita_reset_ioexp(struct device *dev, unsigned char bit)
  119. {
  120. ioexp_output_value &= ~bit;
  121. if (akita_ioexp_device)
  122. schedule_work(&akita_ioexp);
  123. return;
  124. }
  125. EXPORT_SYMBOL(akita_set_ioexp);
  126. EXPORT_SYMBOL(akita_reset_ioexp);
  127. static void akita_ioexp_work(struct work_struct *private_)
  128. {
  129. if (akita_ioexp_device)
  130. max7310_set_ouputs(akita_ioexp_device, ioexp_output_value);
  131. }
  132. #ifdef CONFIG_PM
  133. static int akita_ioexp_suspend(struct platform_device *pdev, pm_message_t state)
  134. {
  135. flush_scheduled_work();
  136. return 0;
  137. }
  138. static int akita_ioexp_resume(struct platform_device *pdev)
  139. {
  140. schedule_work(&akita_ioexp);
  141. return 0;
  142. }
  143. #else
  144. #define akita_ioexp_suspend NULL
  145. #define akita_ioexp_resume NULL
  146. #endif
  147. static int __init akita_ioexp_probe(struct platform_device *pdev)
  148. {
  149. return i2c_add_driver(&max7310_i2c_driver);
  150. }
  151. static int akita_ioexp_remove(struct platform_device *pdev)
  152. {
  153. i2c_del_driver(&max7310_i2c_driver);
  154. return 0;
  155. }
  156. static struct platform_driver akita_ioexp_driver = {
  157. .probe = akita_ioexp_probe,
  158. .remove = akita_ioexp_remove,
  159. .suspend = akita_ioexp_suspend,
  160. .resume = akita_ioexp_resume,
  161. .driver = {
  162. .name = "akita-ioexp",
  163. },
  164. };
  165. static int __init akita_ioexp_init(void)
  166. {
  167. return platform_driver_register(&akita_ioexp_driver);
  168. }
  169. static void __exit akita_ioexp_exit(void)
  170. {
  171. platform_driver_unregister(&akita_ioexp_driver);
  172. }
  173. MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
  174. MODULE_DESCRIPTION("Akita IO-Expander driver");
  175. MODULE_LICENSE("GPL");
  176. fs_initcall(akita_ioexp_init);
  177. module_exit(akita_ioexp_exit);