mcu_mpc8349emitx.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Power Management and GPIO expander driver for MPC8349E-mITX-compatible MCU
  3. *
  4. * Copyright (c) 2008 MontaVista Software, Inc.
  5. *
  6. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  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/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include <linux/mutex.h>
  18. #include <linux/i2c.h>
  19. #include <linux/gpio.h>
  20. #include <linux/of.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/slab.h>
  23. #include <linux/kthread.h>
  24. #include <linux/reboot.h>
  25. #include <asm/prom.h>
  26. #include <asm/machdep.h>
  27. /*
  28. * I don't have specifications for the MCU firmware, I found this register
  29. * and bits positions by the trial&error method.
  30. */
  31. #define MCU_REG_CTRL 0x20
  32. #define MCU_CTRL_POFF 0x40
  33. #define MCU_CTRL_BTN 0x80
  34. #define MCU_NUM_GPIO 2
  35. struct mcu {
  36. struct mutex lock;
  37. struct i2c_client *client;
  38. struct gpio_chip gc;
  39. u8 reg_ctrl;
  40. };
  41. static struct mcu *glob_mcu;
  42. struct task_struct *shutdown_thread;
  43. static int shutdown_thread_fn(void *data)
  44. {
  45. int ret;
  46. struct mcu *mcu = glob_mcu;
  47. while (!kthread_should_stop()) {
  48. ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
  49. if (ret < 0)
  50. pr_err("MCU status reg read failed.\n");
  51. mcu->reg_ctrl = ret;
  52. if (mcu->reg_ctrl & MCU_CTRL_BTN) {
  53. i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL,
  54. mcu->reg_ctrl & ~MCU_CTRL_BTN);
  55. ctrl_alt_del();
  56. }
  57. set_current_state(TASK_INTERRUPTIBLE);
  58. schedule_timeout(HZ);
  59. }
  60. return 0;
  61. }
  62. static ssize_t show_status(struct device *d,
  63. struct device_attribute *attr, char *buf)
  64. {
  65. int ret;
  66. struct mcu *mcu = glob_mcu;
  67. ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
  68. if (ret < 0)
  69. return -ENODEV;
  70. mcu->reg_ctrl = ret;
  71. return sprintf(buf, "%02x\n", ret);
  72. }
  73. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  74. static void mcu_power_off(void)
  75. {
  76. struct mcu *mcu = glob_mcu;
  77. pr_info("Sending power-off request to the MCU...\n");
  78. mutex_lock(&mcu->lock);
  79. i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL,
  80. mcu->reg_ctrl | MCU_CTRL_POFF);
  81. mutex_unlock(&mcu->lock);
  82. }
  83. static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  84. {
  85. struct mcu *mcu = container_of(gc, struct mcu, gc);
  86. u8 bit = 1 << (4 + gpio);
  87. mutex_lock(&mcu->lock);
  88. if (val)
  89. mcu->reg_ctrl &= ~bit;
  90. else
  91. mcu->reg_ctrl |= bit;
  92. i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl);
  93. mutex_unlock(&mcu->lock);
  94. }
  95. static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  96. {
  97. mcu_gpio_set(gc, gpio, val);
  98. return 0;
  99. }
  100. static int mcu_gpiochip_add(struct mcu *mcu)
  101. {
  102. struct device_node *np;
  103. struct gpio_chip *gc = &mcu->gc;
  104. np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx");
  105. if (!np)
  106. return -ENODEV;
  107. gc->owner = THIS_MODULE;
  108. gc->label = np->full_name;
  109. gc->can_sleep = 1;
  110. gc->ngpio = MCU_NUM_GPIO;
  111. gc->base = -1;
  112. gc->set = mcu_gpio_set;
  113. gc->direction_output = mcu_gpio_dir_out;
  114. gc->of_node = np;
  115. return gpiochip_add(gc);
  116. }
  117. static int mcu_gpiochip_remove(struct mcu *mcu)
  118. {
  119. return gpiochip_remove(&mcu->gc);
  120. }
  121. static int __devinit mcu_probe(struct i2c_client *client,
  122. const struct i2c_device_id *id)
  123. {
  124. struct mcu *mcu;
  125. int ret;
  126. mcu = kzalloc(sizeof(*mcu), GFP_KERNEL);
  127. if (!mcu)
  128. return -ENOMEM;
  129. mutex_init(&mcu->lock);
  130. mcu->client = client;
  131. i2c_set_clientdata(client, mcu);
  132. ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
  133. if (ret < 0)
  134. goto err;
  135. mcu->reg_ctrl = ret;
  136. ret = mcu_gpiochip_add(mcu);
  137. if (ret)
  138. goto err;
  139. /* XXX: this is potentially racy, but there is no lock for ppc_md */
  140. if (!ppc_md.power_off) {
  141. glob_mcu = mcu;
  142. ppc_md.power_off = mcu_power_off;
  143. dev_info(&client->dev, "will provide power-off service\n");
  144. }
  145. if (device_create_file(&client->dev, &dev_attr_status))
  146. dev_err(&client->dev,
  147. "couldn't create device file for status\n");
  148. shutdown_thread = kthread_run(shutdown_thread_fn, NULL,
  149. "mcu-i2c-shdn");
  150. return 0;
  151. err:
  152. kfree(mcu);
  153. return ret;
  154. }
  155. static int __devexit mcu_remove(struct i2c_client *client)
  156. {
  157. struct mcu *mcu = i2c_get_clientdata(client);
  158. int ret;
  159. kthread_stop(shutdown_thread);
  160. device_remove_file(&client->dev, &dev_attr_status);
  161. if (glob_mcu == mcu) {
  162. ppc_md.power_off = NULL;
  163. glob_mcu = NULL;
  164. }
  165. ret = mcu_gpiochip_remove(mcu);
  166. if (ret)
  167. return ret;
  168. i2c_set_clientdata(client, NULL);
  169. kfree(mcu);
  170. return 0;
  171. }
  172. static const struct i2c_device_id mcu_ids[] = {
  173. { "mcu-mpc8349emitx", },
  174. {},
  175. };
  176. MODULE_DEVICE_TABLE(i2c, mcu_ids);
  177. static struct of_device_id mcu_of_match_table[] __devinitdata = {
  178. { .compatible = "fsl,mcu-mpc8349emitx", },
  179. { },
  180. };
  181. static struct i2c_driver mcu_driver = {
  182. .driver = {
  183. .name = "mcu-mpc8349emitx",
  184. .owner = THIS_MODULE,
  185. .of_match_table = mcu_of_match_table,
  186. },
  187. .probe = mcu_probe,
  188. .remove = __devexit_p(mcu_remove),
  189. .id_table = mcu_ids,
  190. };
  191. static int __init mcu_init(void)
  192. {
  193. return i2c_add_driver(&mcu_driver);
  194. }
  195. module_init(mcu_init);
  196. static void __exit mcu_exit(void)
  197. {
  198. i2c_del_driver(&mcu_driver);
  199. }
  200. module_exit(mcu_exit);
  201. MODULE_DESCRIPTION("Power Management and GPIO expander driver for "
  202. "MPC8349E-mITX-compatible MCU");
  203. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  204. MODULE_LICENSE("GPL");