mcu_mpc8349emitx.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <asm/prom.h>
  23. #include <asm/machdep.h>
  24. /*
  25. * I don't have specifications for the MCU firmware, I found this register
  26. * and bits positions by the trial&error method.
  27. */
  28. #define MCU_REG_CTRL 0x20
  29. #define MCU_CTRL_POFF 0x40
  30. #define MCU_NUM_GPIO 2
  31. struct mcu {
  32. struct mutex lock;
  33. struct device_node *np;
  34. struct i2c_client *client;
  35. struct of_gpio_chip of_gc;
  36. u8 reg_ctrl;
  37. };
  38. static struct mcu *glob_mcu;
  39. static void mcu_power_off(void)
  40. {
  41. struct mcu *mcu = glob_mcu;
  42. pr_info("Sending power-off request to the MCU...\n");
  43. mutex_lock(&mcu->lock);
  44. i2c_smbus_write_byte_data(glob_mcu->client, MCU_REG_CTRL,
  45. mcu->reg_ctrl | MCU_CTRL_POFF);
  46. mutex_unlock(&mcu->lock);
  47. }
  48. static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  49. {
  50. struct of_gpio_chip *of_gc = to_of_gpio_chip(gc);
  51. struct mcu *mcu = container_of(of_gc, struct mcu, of_gc);
  52. u8 bit = 1 << (4 + gpio);
  53. mutex_lock(&mcu->lock);
  54. if (val)
  55. mcu->reg_ctrl &= ~bit;
  56. else
  57. mcu->reg_ctrl |= bit;
  58. i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl);
  59. mutex_unlock(&mcu->lock);
  60. }
  61. static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  62. {
  63. mcu_gpio_set(gc, gpio, val);
  64. return 0;
  65. }
  66. static int mcu_gpiochip_add(struct mcu *mcu)
  67. {
  68. struct device_node *np;
  69. struct of_gpio_chip *of_gc = &mcu->of_gc;
  70. struct gpio_chip *gc = &of_gc->gc;
  71. int ret;
  72. np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx");
  73. if (!np)
  74. return -ENODEV;
  75. gc->owner = THIS_MODULE;
  76. gc->label = np->full_name;
  77. gc->can_sleep = 1;
  78. gc->ngpio = MCU_NUM_GPIO;
  79. gc->base = -1;
  80. gc->set = mcu_gpio_set;
  81. gc->direction_output = mcu_gpio_dir_out;
  82. of_gc->gpio_cells = 2;
  83. of_gc->xlate = of_gpio_simple_xlate;
  84. np->data = of_gc;
  85. mcu->np = np;
  86. /*
  87. * We don't want to lose the node, its ->data and ->full_name...
  88. * So, if succeeded, we don't put the node here.
  89. */
  90. ret = gpiochip_add(gc);
  91. if (ret)
  92. of_node_put(np);
  93. return ret;
  94. }
  95. static int mcu_gpiochip_remove(struct mcu *mcu)
  96. {
  97. int ret;
  98. ret = gpiochip_remove(&mcu->of_gc.gc);
  99. if (ret)
  100. return ret;
  101. of_node_put(mcu->np);
  102. return 0;
  103. }
  104. static int __devinit mcu_probe(struct i2c_client *client,
  105. const struct i2c_device_id *id)
  106. {
  107. struct mcu *mcu;
  108. int ret;
  109. mcu = kzalloc(sizeof(*mcu), GFP_KERNEL);
  110. if (!mcu)
  111. return -ENOMEM;
  112. mutex_init(&mcu->lock);
  113. mcu->client = client;
  114. i2c_set_clientdata(client, mcu);
  115. ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
  116. if (ret < 0)
  117. goto err;
  118. mcu->reg_ctrl = ret;
  119. ret = mcu_gpiochip_add(mcu);
  120. if (ret)
  121. goto err;
  122. /* XXX: this is potentially racy, but there is no lock for ppc_md */
  123. if (!ppc_md.power_off) {
  124. glob_mcu = mcu;
  125. ppc_md.power_off = mcu_power_off;
  126. dev_info(&client->dev, "will provide power-off service\n");
  127. }
  128. return 0;
  129. err:
  130. kfree(mcu);
  131. return ret;
  132. }
  133. static int __devexit mcu_remove(struct i2c_client *client)
  134. {
  135. struct mcu *mcu = i2c_get_clientdata(client);
  136. int ret;
  137. if (glob_mcu == mcu) {
  138. ppc_md.power_off = NULL;
  139. glob_mcu = NULL;
  140. }
  141. ret = mcu_gpiochip_remove(mcu);
  142. if (ret)
  143. return ret;
  144. i2c_set_clientdata(client, NULL);
  145. kfree(mcu);
  146. return 0;
  147. }
  148. static const struct i2c_device_id mcu_ids[] = {
  149. { "mcu-mpc8349emitx", },
  150. {},
  151. };
  152. MODULE_DEVICE_TABLE(i2c, mcu_ids);
  153. static struct i2c_driver mcu_driver = {
  154. .driver = {
  155. .name = "mcu-mpc8349emitx",
  156. .owner = THIS_MODULE,
  157. },
  158. .probe = mcu_probe,
  159. .remove = __devexit_p(mcu_remove),
  160. .id_table = mcu_ids,
  161. };
  162. static int __init mcu_init(void)
  163. {
  164. return i2c_add_driver(&mcu_driver);
  165. }
  166. module_init(mcu_init);
  167. static void __exit mcu_exit(void)
  168. {
  169. i2c_del_driver(&mcu_driver);
  170. }
  171. module_exit(mcu_exit);
  172. MODULE_DESCRIPTION("Power Management and GPIO expander driver for "
  173. "MPC8349E-mITX-compatible MCU");
  174. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  175. MODULE_LICENSE("GPL");