1ea0105eaSAnton Vorontsov /* 2ea0105eaSAnton Vorontsov * Power Management and GPIO expander driver for MPC8349E-mITX-compatible MCU 3ea0105eaSAnton Vorontsov * 4ea0105eaSAnton Vorontsov * Copyright (c) 2008 MontaVista Software, Inc. 5ea0105eaSAnton Vorontsov * 6ea0105eaSAnton Vorontsov * Author: Anton Vorontsov <avorontsov@ru.mvista.com> 7ea0105eaSAnton Vorontsov * 8ea0105eaSAnton Vorontsov * This program is free software; you can redistribute it and/or modify 9ea0105eaSAnton Vorontsov * it under the terms of the GNU General Public License as published by 10ea0105eaSAnton Vorontsov * the Free Software Foundation; either version 2 of the License, or 11ea0105eaSAnton Vorontsov * (at your option) any later version. 12ea0105eaSAnton Vorontsov */ 13ea0105eaSAnton Vorontsov 14ea0105eaSAnton Vorontsov #include <linux/init.h> 15ea0105eaSAnton Vorontsov #include <linux/kernel.h> 16ea0105eaSAnton Vorontsov #include <linux/module.h> 17ea0105eaSAnton Vorontsov #include <linux/device.h> 18ea0105eaSAnton Vorontsov #include <linux/mutex.h> 19ea0105eaSAnton Vorontsov #include <linux/i2c.h> 20ea0105eaSAnton Vorontsov #include <linux/gpio.h> 21ea0105eaSAnton Vorontsov #include <linux/of.h> 22ea0105eaSAnton Vorontsov #include <linux/of_gpio.h> 23*5a0e3ad6STejun Heo #include <linux/slab.h> 24ea0105eaSAnton Vorontsov #include <asm/prom.h> 25ea0105eaSAnton Vorontsov #include <asm/machdep.h> 26ea0105eaSAnton Vorontsov 27ea0105eaSAnton Vorontsov /* 28ea0105eaSAnton Vorontsov * I don't have specifications for the MCU firmware, I found this register 29ea0105eaSAnton Vorontsov * and bits positions by the trial&error method. 30ea0105eaSAnton Vorontsov */ 31ea0105eaSAnton Vorontsov #define MCU_REG_CTRL 0x20 32ea0105eaSAnton Vorontsov #define MCU_CTRL_POFF 0x40 33ea0105eaSAnton Vorontsov 34ea0105eaSAnton Vorontsov #define MCU_NUM_GPIO 2 35ea0105eaSAnton Vorontsov 36ea0105eaSAnton Vorontsov struct mcu { 37ea0105eaSAnton Vorontsov struct mutex lock; 38ea0105eaSAnton Vorontsov struct device_node *np; 39ea0105eaSAnton Vorontsov struct i2c_client *client; 40ea0105eaSAnton Vorontsov struct of_gpio_chip of_gc; 41ea0105eaSAnton Vorontsov u8 reg_ctrl; 42ea0105eaSAnton Vorontsov }; 43ea0105eaSAnton Vorontsov 44ea0105eaSAnton Vorontsov static struct mcu *glob_mcu; 45ea0105eaSAnton Vorontsov 46ea0105eaSAnton Vorontsov static void mcu_power_off(void) 47ea0105eaSAnton Vorontsov { 48ea0105eaSAnton Vorontsov struct mcu *mcu = glob_mcu; 49ea0105eaSAnton Vorontsov 50ea0105eaSAnton Vorontsov pr_info("Sending power-off request to the MCU...\n"); 51ea0105eaSAnton Vorontsov mutex_lock(&mcu->lock); 52ea0105eaSAnton Vorontsov i2c_smbus_write_byte_data(glob_mcu->client, MCU_REG_CTRL, 53ea0105eaSAnton Vorontsov mcu->reg_ctrl | MCU_CTRL_POFF); 54ea0105eaSAnton Vorontsov mutex_unlock(&mcu->lock); 55ea0105eaSAnton Vorontsov } 56ea0105eaSAnton Vorontsov 57ea0105eaSAnton Vorontsov static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val) 58ea0105eaSAnton Vorontsov { 59ea0105eaSAnton Vorontsov struct of_gpio_chip *of_gc = to_of_gpio_chip(gc); 60ea0105eaSAnton Vorontsov struct mcu *mcu = container_of(of_gc, struct mcu, of_gc); 61ea0105eaSAnton Vorontsov u8 bit = 1 << (4 + gpio); 62ea0105eaSAnton Vorontsov 63ea0105eaSAnton Vorontsov mutex_lock(&mcu->lock); 64ea0105eaSAnton Vorontsov if (val) 65ea0105eaSAnton Vorontsov mcu->reg_ctrl &= ~bit; 66ea0105eaSAnton Vorontsov else 67ea0105eaSAnton Vorontsov mcu->reg_ctrl |= bit; 68ea0105eaSAnton Vorontsov 69ea0105eaSAnton Vorontsov i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl); 70ea0105eaSAnton Vorontsov mutex_unlock(&mcu->lock); 71ea0105eaSAnton Vorontsov } 72ea0105eaSAnton Vorontsov 73ea0105eaSAnton Vorontsov static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) 74ea0105eaSAnton Vorontsov { 75ea0105eaSAnton Vorontsov mcu_gpio_set(gc, gpio, val); 76ea0105eaSAnton Vorontsov return 0; 77ea0105eaSAnton Vorontsov } 78ea0105eaSAnton Vorontsov 79ea0105eaSAnton Vorontsov static int mcu_gpiochip_add(struct mcu *mcu) 80ea0105eaSAnton Vorontsov { 81ea0105eaSAnton Vorontsov struct device_node *np; 82ea0105eaSAnton Vorontsov struct of_gpio_chip *of_gc = &mcu->of_gc; 83ea0105eaSAnton Vorontsov struct gpio_chip *gc = &of_gc->gc; 84ea0105eaSAnton Vorontsov int ret; 85ea0105eaSAnton Vorontsov 86ea0105eaSAnton Vorontsov np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx"); 87ea0105eaSAnton Vorontsov if (!np) 88ea0105eaSAnton Vorontsov return -ENODEV; 89ea0105eaSAnton Vorontsov 90ea0105eaSAnton Vorontsov gc->owner = THIS_MODULE; 91ea0105eaSAnton Vorontsov gc->label = np->full_name; 92ea0105eaSAnton Vorontsov gc->can_sleep = 1; 93ea0105eaSAnton Vorontsov gc->ngpio = MCU_NUM_GPIO; 94ea0105eaSAnton Vorontsov gc->base = -1; 95ea0105eaSAnton Vorontsov gc->set = mcu_gpio_set; 96ea0105eaSAnton Vorontsov gc->direction_output = mcu_gpio_dir_out; 97ea0105eaSAnton Vorontsov of_gc->gpio_cells = 2; 98ea0105eaSAnton Vorontsov of_gc->xlate = of_gpio_simple_xlate; 99ea0105eaSAnton Vorontsov 100ea0105eaSAnton Vorontsov np->data = of_gc; 101ea0105eaSAnton Vorontsov mcu->np = np; 102ea0105eaSAnton Vorontsov 103ea0105eaSAnton Vorontsov /* 104ea0105eaSAnton Vorontsov * We don't want to lose the node, its ->data and ->full_name... 105ea0105eaSAnton Vorontsov * So, if succeeded, we don't put the node here. 106ea0105eaSAnton Vorontsov */ 107ea0105eaSAnton Vorontsov ret = gpiochip_add(gc); 108ea0105eaSAnton Vorontsov if (ret) 109ea0105eaSAnton Vorontsov of_node_put(np); 110ea0105eaSAnton Vorontsov return ret; 111ea0105eaSAnton Vorontsov } 112ea0105eaSAnton Vorontsov 113ea0105eaSAnton Vorontsov static int mcu_gpiochip_remove(struct mcu *mcu) 114ea0105eaSAnton Vorontsov { 115ea0105eaSAnton Vorontsov int ret; 116ea0105eaSAnton Vorontsov 117ea0105eaSAnton Vorontsov ret = gpiochip_remove(&mcu->of_gc.gc); 118ea0105eaSAnton Vorontsov if (ret) 119ea0105eaSAnton Vorontsov return ret; 120ea0105eaSAnton Vorontsov of_node_put(mcu->np); 121ea0105eaSAnton Vorontsov 122ea0105eaSAnton Vorontsov return 0; 123ea0105eaSAnton Vorontsov } 124ea0105eaSAnton Vorontsov 125ea0105eaSAnton Vorontsov static int __devinit mcu_probe(struct i2c_client *client, 126ea0105eaSAnton Vorontsov const struct i2c_device_id *id) 127ea0105eaSAnton Vorontsov { 128ea0105eaSAnton Vorontsov struct mcu *mcu; 129ea0105eaSAnton Vorontsov int ret; 130ea0105eaSAnton Vorontsov 131ea0105eaSAnton Vorontsov mcu = kzalloc(sizeof(*mcu), GFP_KERNEL); 132ea0105eaSAnton Vorontsov if (!mcu) 133ea0105eaSAnton Vorontsov return -ENOMEM; 134ea0105eaSAnton Vorontsov 135ea0105eaSAnton Vorontsov mutex_init(&mcu->lock); 136ea0105eaSAnton Vorontsov mcu->client = client; 137ea0105eaSAnton Vorontsov i2c_set_clientdata(client, mcu); 138ea0105eaSAnton Vorontsov 139ea0105eaSAnton Vorontsov ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL); 140ea0105eaSAnton Vorontsov if (ret < 0) 141ea0105eaSAnton Vorontsov goto err; 142ea0105eaSAnton Vorontsov mcu->reg_ctrl = ret; 143ea0105eaSAnton Vorontsov 144ea0105eaSAnton Vorontsov ret = mcu_gpiochip_add(mcu); 145ea0105eaSAnton Vorontsov if (ret) 146ea0105eaSAnton Vorontsov goto err; 147ea0105eaSAnton Vorontsov 148ea0105eaSAnton Vorontsov /* XXX: this is potentially racy, but there is no lock for ppc_md */ 149ea0105eaSAnton Vorontsov if (!ppc_md.power_off) { 150ea0105eaSAnton Vorontsov glob_mcu = mcu; 151ea0105eaSAnton Vorontsov ppc_md.power_off = mcu_power_off; 152ea0105eaSAnton Vorontsov dev_info(&client->dev, "will provide power-off service\n"); 153ea0105eaSAnton Vorontsov } 154ea0105eaSAnton Vorontsov 155ea0105eaSAnton Vorontsov return 0; 156ea0105eaSAnton Vorontsov err: 157ea0105eaSAnton Vorontsov kfree(mcu); 158ea0105eaSAnton Vorontsov return ret; 159ea0105eaSAnton Vorontsov } 160ea0105eaSAnton Vorontsov 161ea0105eaSAnton Vorontsov static int __devexit mcu_remove(struct i2c_client *client) 162ea0105eaSAnton Vorontsov { 163ea0105eaSAnton Vorontsov struct mcu *mcu = i2c_get_clientdata(client); 164ea0105eaSAnton Vorontsov int ret; 165ea0105eaSAnton Vorontsov 166ea0105eaSAnton Vorontsov if (glob_mcu == mcu) { 167ea0105eaSAnton Vorontsov ppc_md.power_off = NULL; 168ea0105eaSAnton Vorontsov glob_mcu = NULL; 169ea0105eaSAnton Vorontsov } 170ea0105eaSAnton Vorontsov 171ea0105eaSAnton Vorontsov ret = mcu_gpiochip_remove(mcu); 172ea0105eaSAnton Vorontsov if (ret) 173ea0105eaSAnton Vorontsov return ret; 174ea0105eaSAnton Vorontsov i2c_set_clientdata(client, NULL); 175ea0105eaSAnton Vorontsov kfree(mcu); 176ea0105eaSAnton Vorontsov return 0; 177ea0105eaSAnton Vorontsov } 178ea0105eaSAnton Vorontsov 179ea0105eaSAnton Vorontsov static const struct i2c_device_id mcu_ids[] = { 180ea0105eaSAnton Vorontsov { "mcu-mpc8349emitx", }, 181ea0105eaSAnton Vorontsov {}, 182ea0105eaSAnton Vorontsov }; 183ea0105eaSAnton Vorontsov MODULE_DEVICE_TABLE(i2c, mcu_ids); 184ea0105eaSAnton Vorontsov 185ea0105eaSAnton Vorontsov static struct i2c_driver mcu_driver = { 186ea0105eaSAnton Vorontsov .driver = { 187ea0105eaSAnton Vorontsov .name = "mcu-mpc8349emitx", 188ea0105eaSAnton Vorontsov .owner = THIS_MODULE, 189ea0105eaSAnton Vorontsov }, 190ea0105eaSAnton Vorontsov .probe = mcu_probe, 191ea0105eaSAnton Vorontsov .remove = __devexit_p(mcu_remove), 192ea0105eaSAnton Vorontsov .id_table = mcu_ids, 193ea0105eaSAnton Vorontsov }; 194ea0105eaSAnton Vorontsov 195ea0105eaSAnton Vorontsov static int __init mcu_init(void) 196ea0105eaSAnton Vorontsov { 197ea0105eaSAnton Vorontsov return i2c_add_driver(&mcu_driver); 198ea0105eaSAnton Vorontsov } 199ea0105eaSAnton Vorontsov module_init(mcu_init); 200ea0105eaSAnton Vorontsov 201ea0105eaSAnton Vorontsov static void __exit mcu_exit(void) 202ea0105eaSAnton Vorontsov { 203ea0105eaSAnton Vorontsov i2c_del_driver(&mcu_driver); 204ea0105eaSAnton Vorontsov } 205ea0105eaSAnton Vorontsov module_exit(mcu_exit); 206ea0105eaSAnton Vorontsov 207ea0105eaSAnton Vorontsov MODULE_DESCRIPTION("Power Management and GPIO expander driver for " 208ea0105eaSAnton Vorontsov "MPC8349E-mITX-compatible MCU"); 209ea0105eaSAnton Vorontsov MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>"); 210ea0105eaSAnton Vorontsov MODULE_LICENSE("GPL"); 211