mcu_mpc8349emitx.c 5.1 KB

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