Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Quanta EC driver for the Winbond Embedded Controller |
| 2 | * |
| 3 | * Copyright (C) 2009 Quanta Computer Inc. |
| 4 | * |
| 5 | * This software is licensed under the terms of the GNU General Public |
| 6 | * License version 2, as published by the Free Software Foundation, and |
| 7 | * may be copied, distributed, and modified under those terms. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/slab.h> |
| 20 | |
| 21 | #define EC_ID_NAME "qci-i2cec" |
| 22 | #define EC_BUFFER_LEN 16 |
| 23 | #define EC_CMD_POWER_OFF 0xAC |
| 24 | #define EC_CMD_RESTART 0xAB |
| 25 | |
| 26 | static struct i2c_client *g_i2cec_client; |
| 27 | |
| 28 | /* General structure to hold the driver data */ |
| 29 | struct i2cec_drv_data { |
| 30 | struct i2c_client *i2cec_client; |
| 31 | struct work_struct work; |
| 32 | char ec_data[EC_BUFFER_LEN+1]; |
| 33 | }; |
| 34 | |
| 35 | static int __devinit wpce_probe(struct i2c_client *client, |
| 36 | const struct i2c_device_id *id); |
| 37 | static int __devexit wpce_remove(struct i2c_client *kbd); |
| 38 | |
| 39 | #ifdef CONFIG_PM |
| 40 | static int wpce_suspend(struct device *dev) |
| 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static int wpce_resume(struct device *dev) |
| 46 | { |
| 47 | return 0; |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | #ifdef CONFIG_PM |
| 52 | static struct dev_pm_ops wpce_pm_ops = { |
| 53 | .suspend = wpce_suspend, |
| 54 | .resume = wpce_resume, |
| 55 | }; |
| 56 | #endif |
| 57 | |
| 58 | static const struct i2c_device_id wpce_idtable[] = { |
| 59 | { EC_ID_NAME, 0 }, |
| 60 | { } |
| 61 | }; |
| 62 | |
| 63 | static struct i2c_driver wpce_driver = { |
| 64 | .driver = { |
| 65 | .owner = THIS_MODULE, |
| 66 | .name = EC_ID_NAME, |
| 67 | #ifdef CONFIG_PM |
| 68 | .pm = &wpce_pm_ops, |
| 69 | #endif |
| 70 | }, |
| 71 | .probe = wpce_probe, |
| 72 | .remove = __devexit_p(wpce_remove), |
| 73 | .id_table = wpce_idtable, |
| 74 | }; |
| 75 | |
| 76 | static int __devinit wpce_probe(struct i2c_client *client, |
| 77 | const struct i2c_device_id *id) |
| 78 | { |
| 79 | int err = -ENOMEM; |
| 80 | struct i2cec_drv_data *context = 0; |
| 81 | |
| 82 | /* there is no need to call i2c_check_functionality() since it is the |
| 83 | client's job to use the interface (I2C vs SMBUS) appropriate for it. */ |
| 84 | client->driver = &wpce_driver; |
| 85 | context = kzalloc(sizeof(struct i2cec_drv_data), GFP_KERNEL); |
| 86 | if (!context) |
| 87 | return err; |
| 88 | |
| 89 | context->i2cec_client = client; |
| 90 | g_i2cec_client = client; |
| 91 | i2c_set_clientdata(context->i2cec_client, context); |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int __devexit wpce_remove(struct i2c_client *dev) |
| 97 | { |
| 98 | struct i2cec_drv_data *context = i2c_get_clientdata(dev); |
| 99 | g_i2cec_client = NULL; |
| 100 | kfree(context); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int __init wpce_init(void) |
| 106 | { |
| 107 | return i2c_add_driver(&wpce_driver); |
| 108 | } |
| 109 | |
| 110 | static void __exit wpce_exit(void) |
| 111 | { |
| 112 | i2c_del_driver(&wpce_driver); |
| 113 | } |
| 114 | |
| 115 | struct i2c_client *wpce_get_i2c_client(void) |
| 116 | { |
| 117 | return g_i2cec_client; |
| 118 | } |
| 119 | EXPORT_SYMBOL_GPL(wpce_get_i2c_client); |
| 120 | |
| 121 | void wpce_poweroff(void) |
| 122 | { |
| 123 | if (g_i2cec_client == NULL) |
| 124 | return; |
| 125 | i2c_smbus_write_byte(g_i2cec_client, EC_CMD_POWER_OFF); |
| 126 | } |
| 127 | EXPORT_SYMBOL_GPL(wpce_poweroff); |
| 128 | |
| 129 | void wpce_restart(void) |
| 130 | { |
| 131 | if (g_i2cec_client == NULL) |
| 132 | return; |
| 133 | i2c_smbus_write_byte(g_i2cec_client, EC_CMD_RESTART); |
| 134 | } |
| 135 | EXPORT_SYMBOL_GPL(wpce_restart); |
| 136 | |
| 137 | int wpce_i2c_transfer(struct i2c_msg *msg) |
| 138 | { |
| 139 | if (g_i2cec_client == NULL) |
| 140 | return -1; |
| 141 | msg->addr = g_i2cec_client->addr; |
| 142 | return i2c_transfer(g_i2cec_client->adapter, msg, 1); |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(wpce_i2c_transfer); |
| 145 | |
| 146 | int wpce_smbus_write_word_data(u8 command, u16 value) |
| 147 | { |
| 148 | if (g_i2cec_client == NULL) |
| 149 | return -1; |
| 150 | return i2c_smbus_write_word_data(g_i2cec_client, command, value); |
| 151 | } |
| 152 | EXPORT_SYMBOL_GPL(wpce_smbus_write_word_data); |
| 153 | |
| 154 | int wpce_smbus_write_byte_data(u8 command, u8 value) |
| 155 | { |
| 156 | if (g_i2cec_client == NULL) |
| 157 | return -1; |
| 158 | return i2c_smbus_write_byte_data(g_i2cec_client, command, value); |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(wpce_smbus_write_byte_data); |
| 161 | |
| 162 | module_init(wpce_init); |
| 163 | module_exit(wpce_exit); |
| 164 | |
| 165 | MODULE_AUTHOR("Quanta Computer Inc."); |
| 166 | MODULE_DESCRIPTION("Quanta Embedded Controller I2C Bridge Driver"); |
| 167 | MODULE_LICENSE("GPL v2"); |