mcu_mpc8349emitx.c 4.3 KB

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