blob: 2d007003cc29262dee912a4efcf2a870c6de2faf [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* 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
26static struct i2c_client *g_i2cec_client;
27
28/* General structure to hold the driver data */
29struct i2cec_drv_data {
30 struct i2c_client *i2cec_client;
31 struct work_struct work;
32 char ec_data[EC_BUFFER_LEN+1];
33};
34
35static int __devinit wpce_probe(struct i2c_client *client,
36 const struct i2c_device_id *id);
37static int __devexit wpce_remove(struct i2c_client *kbd);
38
39#ifdef CONFIG_PM
40static int wpce_suspend(struct device *dev)
41{
42 return 0;
43}
44
45static int wpce_resume(struct device *dev)
46{
47 return 0;
48}
49#endif
50
51#ifdef CONFIG_PM
52static struct dev_pm_ops wpce_pm_ops = {
53 .suspend = wpce_suspend,
54 .resume = wpce_resume,
55};
56#endif
57
58static const struct i2c_device_id wpce_idtable[] = {
59 { EC_ID_NAME, 0 },
60 { }
61};
62
63static 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
76static 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
96static 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
105static int __init wpce_init(void)
106{
107 return i2c_add_driver(&wpce_driver);
108}
109
110static void __exit wpce_exit(void)
111{
112 i2c_del_driver(&wpce_driver);
113}
114
115struct i2c_client *wpce_get_i2c_client(void)
116{
117 return g_i2cec_client;
118}
119EXPORT_SYMBOL_GPL(wpce_get_i2c_client);
120
121void 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}
127EXPORT_SYMBOL_GPL(wpce_poweroff);
128
129void wpce_restart(void)
130{
131 if (g_i2cec_client == NULL)
132 return;
133 i2c_smbus_write_byte(g_i2cec_client, EC_CMD_RESTART);
134}
135EXPORT_SYMBOL_GPL(wpce_restart);
136
137int 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}
144EXPORT_SYMBOL_GPL(wpce_i2c_transfer);
145
146int 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}
152EXPORT_SYMBOL_GPL(wpce_smbus_write_word_data);
153
154int 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}
160EXPORT_SYMBOL_GPL(wpce_smbus_write_byte_data);
161
162module_init(wpce_init);
163module_exit(wpce_exit);
164
165MODULE_AUTHOR("Quanta Computer Inc.");
166MODULE_DESCRIPTION("Quanta Embedded Controller I2C Bridge Driver");
167MODULE_LICENSE("GPL v2");