3 Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @addtogroup COMPASSDL
25 * @brief Magnetometer setup and handling methods for Alps HSCDTD002B
29 /* -------------------------------------------------------------------------- */
31 #include <linux/i2c.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/kernel.h>
35 #include <linux/errno.h>
36 #include <linux/slab.h>
37 #include <linux/delay.h>
41 #include <linux/mpu.h>
45 #define MPL_LOG_TAG "MPL-compass"
47 /* -------------------------------------------------------------------------- */
48 #define COMPASS_HSCDTD002B_STAT (0x18)
49 #define COMPASS_HSCDTD002B_CTRL1 (0x1B)
50 #define COMPASS_HSCDTD002B_CTRL2 (0x1C)
51 #define COMPASS_HSCDTD002B_CTRL3 (0x1D)
52 #define COMPASS_HSCDTD002B_DATAX (0x10)
54 /* -------------------------------------------------------------------------- */
55 static int hscdtd002b_suspend(void *mlsl_handle,
56 struct ext_slave_descr *slave,
57 struct ext_slave_platform_data *pdata)
59 int result = INV_SUCCESS;
61 /* Power mode: stand-by */
63 inv_serial_single_write(mlsl_handle, pdata->address,
64 COMPASS_HSCDTD002B_CTRL1, 0x00);
66 LOG_RESULT_LOCATION(result);
69 msleep(1); /* turn-off time */
74 static int hscdtd002b_resume(void *mlsl_handle,
75 struct ext_slave_descr *slave,
76 struct ext_slave_platform_data *pdata)
78 int result = INV_SUCCESS;
82 inv_serial_single_write(mlsl_handle, pdata->address,
83 COMPASS_HSCDTD002B_CTRL3, 0x80);
85 LOG_RESULT_LOCATION(result);
88 /* Force state; Power mode: active */
90 inv_serial_single_write(mlsl_handle, pdata->address,
91 COMPASS_HSCDTD002B_CTRL1, 0x82);
93 LOG_RESULT_LOCATION(result);
96 /* Data ready enable */
98 inv_serial_single_write(mlsl_handle, pdata->address,
99 COMPASS_HSCDTD002B_CTRL2, 0x08);
101 LOG_RESULT_LOCATION(result);
104 msleep(1); /* turn-on time */
109 static int hscdtd002b_read(void *mlsl_handle,
110 struct ext_slave_descr *slave,
111 struct ext_slave_platform_data *pdata,
115 int result = INV_SUCCESS;
116 int status = INV_SUCCESS;
118 /* Read status reg. to check if data is ready */
120 inv_serial_read(mlsl_handle, pdata->address,
121 COMPASS_HSCDTD002B_STAT, 1, &stat);
123 LOG_RESULT_LOCATION(result);
128 inv_serial_read(mlsl_handle, pdata->address,
129 COMPASS_HSCDTD002B_DATAX, 6,
130 (unsigned char *)data);
132 LOG_RESULT_LOCATION(result);
135 status = INV_SUCCESS;
136 } else if (stat & 0x20) {
137 status = INV_ERROR_COMPASS_DATA_OVERFLOW;
139 status = INV_ERROR_COMPASS_DATA_NOT_READY;
141 /* trigger next measurement read */
143 inv_serial_single_write(mlsl_handle, pdata->address,
144 COMPASS_HSCDTD002B_CTRL3, 0x40);
146 LOG_RESULT_LOCATION(result);
153 static struct ext_slave_descr hscdtd002b_descr = {
156 .suspend = hscdtd002b_suspend,
157 .resume = hscdtd002b_resume,
158 .read = hscdtd002b_read,
161 .name = "hscdtd002b",
162 .type = EXT_SLAVE_TYPE_COMPASS,
163 .id = COMPASS_ID_HSCDTD002B,
166 .endian = EXT_SLAVE_LITTLE_ENDIAN,
167 .range = {9830, 4000},
172 struct ext_slave_descr *hscdtd002b_get_slave_descr(void)
174 return &hscdtd002b_descr;
177 /* -------------------------------------------------------------------------- */
178 struct hscdtd002b_mod_private_data {
179 struct i2c_client *client;
180 struct ext_slave_platform_data *pdata;
183 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
185 static int hscdtd002b_mod_probe(struct i2c_client *client,
186 const struct i2c_device_id *devid)
188 struct ext_slave_platform_data *pdata;
189 struct hscdtd002b_mod_private_data *private_data;
192 dev_info(&client->adapter->dev, "%s: %s\n", __func__, devid->name);
194 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
199 pdata = client->dev.platform_data;
201 dev_err(&client->adapter->dev,
202 "Missing platform data for slave %s\n", devid->name);
207 private_data = kzalloc(sizeof(*private_data), GFP_KERNEL);
213 i2c_set_clientdata(client, private_data);
214 private_data->client = client;
215 private_data->pdata = pdata;
217 result = inv_mpu_register_slave(THIS_MODULE, client, pdata,
218 hscdtd002b_get_slave_descr);
220 dev_err(&client->adapter->dev,
221 "Slave registration failed: %s, %d\n",
222 devid->name, result);
223 goto out_free_memory;
231 dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result);
236 static int hscdtd002b_mod_remove(struct i2c_client *client)
238 struct hscdtd002b_mod_private_data *private_data =
239 i2c_get_clientdata(client);
241 dev_dbg(&client->adapter->dev, "%s\n", __func__);
243 inv_mpu_unregister_slave(client, private_data->pdata,
244 hscdtd002b_get_slave_descr);
250 static const struct i2c_device_id hscdtd002b_mod_id[] = {
251 { "hscdtd002b", COMPASS_ID_HSCDTD002B },
255 MODULE_DEVICE_TABLE(i2c, hscdtd002b_mod_id);
257 static struct i2c_driver hscdtd002b_mod_driver = {
258 .class = I2C_CLASS_HWMON,
259 .probe = hscdtd002b_mod_probe,
260 .remove = hscdtd002b_mod_remove,
261 .id_table = hscdtd002b_mod_id,
263 .owner = THIS_MODULE,
264 .name = "hscdtd002b_mod",
266 .address_list = normal_i2c,
269 static int __init hscdtd002b_mod_init(void)
271 int res = i2c_add_driver(&hscdtd002b_mod_driver);
272 pr_info("%s: Probe name %s\n", __func__, "hscdtd002b_mod");
274 pr_err("%s failed\n", __func__);
278 static void __exit hscdtd002b_mod_exit(void)
280 pr_info("%s\n", __func__);
281 i2c_del_driver(&hscdtd002b_mod_driver);
284 module_init(hscdtd002b_mod_init);
285 module_exit(hscdtd002b_mod_exit);
287 MODULE_AUTHOR("Invensense Corporation");
288 MODULE_DESCRIPTION("Driver to integrate HSCDTD002B sensor with the MPU");
289 MODULE_LICENSE("GPL");
290 MODULE_ALIAS("hscdtd002b_mod");