Path: blob/master/arch/powerpc/platforms/83xx/mcu_mpc8349emitx.c
10818 views
/*1* Power Management and GPIO expander driver for MPC8349E-mITX-compatible MCU2*3* Copyright (c) 2008 MontaVista Software, Inc.4*5* Author: Anton Vorontsov <[email protected]>6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2 of the License, or10* (at your option) any later version.11*/1213#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>2526/*27* I don't have specifications for the MCU firmware, I found this register28* and bits positions by the trial&error method.29*/30#define MCU_REG_CTRL 0x2031#define MCU_CTRL_POFF 0x403233#define MCU_NUM_GPIO 23435struct mcu {36struct mutex lock;37struct i2c_client *client;38struct gpio_chip gc;39u8 reg_ctrl;40};4142static struct mcu *glob_mcu;4344static void mcu_power_off(void)45{46struct mcu *mcu = glob_mcu;4748pr_info("Sending power-off request to the MCU...\n");49mutex_lock(&mcu->lock);50i2c_smbus_write_byte_data(glob_mcu->client, MCU_REG_CTRL,51mcu->reg_ctrl | MCU_CTRL_POFF);52mutex_unlock(&mcu->lock);53}5455static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)56{57struct mcu *mcu = container_of(gc, struct mcu, gc);58u8 bit = 1 << (4 + gpio);5960mutex_lock(&mcu->lock);61if (val)62mcu->reg_ctrl &= ~bit;63else64mcu->reg_ctrl |= bit;6566i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl);67mutex_unlock(&mcu->lock);68}6970static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)71{72mcu_gpio_set(gc, gpio, val);73return 0;74}7576static int mcu_gpiochip_add(struct mcu *mcu)77{78struct device_node *np;79struct gpio_chip *gc = &mcu->gc;8081np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx");82if (!np)83return -ENODEV;8485gc->owner = THIS_MODULE;86gc->label = np->full_name;87gc->can_sleep = 1;88gc->ngpio = MCU_NUM_GPIO;89gc->base = -1;90gc->set = mcu_gpio_set;91gc->direction_output = mcu_gpio_dir_out;92gc->of_node = np;9394return gpiochip_add(gc);95}9697static int mcu_gpiochip_remove(struct mcu *mcu)98{99return gpiochip_remove(&mcu->gc);100}101102static int __devinit mcu_probe(struct i2c_client *client,103const struct i2c_device_id *id)104{105struct mcu *mcu;106int ret;107108mcu = kzalloc(sizeof(*mcu), GFP_KERNEL);109if (!mcu)110return -ENOMEM;111112mutex_init(&mcu->lock);113mcu->client = client;114i2c_set_clientdata(client, mcu);115116ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);117if (ret < 0)118goto err;119mcu->reg_ctrl = ret;120121ret = mcu_gpiochip_add(mcu);122if (ret)123goto err;124125/* XXX: this is potentially racy, but there is no lock for ppc_md */126if (!ppc_md.power_off) {127glob_mcu = mcu;128ppc_md.power_off = mcu_power_off;129dev_info(&client->dev, "will provide power-off service\n");130}131132return 0;133err:134kfree(mcu);135return ret;136}137138static int __devexit mcu_remove(struct i2c_client *client)139{140struct mcu *mcu = i2c_get_clientdata(client);141int ret;142143if (glob_mcu == mcu) {144ppc_md.power_off = NULL;145glob_mcu = NULL;146}147148ret = mcu_gpiochip_remove(mcu);149if (ret)150return ret;151i2c_set_clientdata(client, NULL);152kfree(mcu);153return 0;154}155156static const struct i2c_device_id mcu_ids[] = {157{ "mcu-mpc8349emitx", },158{},159};160MODULE_DEVICE_TABLE(i2c, mcu_ids);161162static struct of_device_id mcu_of_match_table[] __devinitdata = {163{ .compatible = "fsl,mcu-mpc8349emitx", },164{ },165};166167static struct i2c_driver mcu_driver = {168.driver = {169.name = "mcu-mpc8349emitx",170.owner = THIS_MODULE,171.of_match_table = mcu_of_match_table,172},173.probe = mcu_probe,174.remove = __devexit_p(mcu_remove),175.id_table = mcu_ids,176};177178static int __init mcu_init(void)179{180return i2c_add_driver(&mcu_driver);181}182module_init(mcu_init);183184static void __exit mcu_exit(void)185{186i2c_del_driver(&mcu_driver);187}188module_exit(mcu_exit);189190MODULE_DESCRIPTION("Power Management and GPIO expander driver for "191"MPC8349E-mITX-compatible MCU");192MODULE_AUTHOR("Anton Vorontsov <[email protected]>");193MODULE_LICENSE("GPL");194195196