diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
| commit | fcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch) | |
| tree | a57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/misc/inv_mpu/compass/mmc314x.c | |
| parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) | |
Diffstat (limited to 'drivers/misc/inv_mpu/compass/mmc314x.c')
| -rw-r--r-- | drivers/misc/inv_mpu/compass/mmc314x.c | 313 |
1 files changed, 313 insertions, 0 deletions
diff --git a/drivers/misc/inv_mpu/compass/mmc314x.c b/drivers/misc/inv_mpu/compass/mmc314x.c new file mode 100644 index 00000000000..786fadcc3e4 --- /dev/null +++ b/drivers/misc/inv_mpu/compass/mmc314x.c | |||
| @@ -0,0 +1,313 @@ | |||
| 1 | /* | ||
| 2 | $License: | ||
| 3 | Copyright (C) 2011 InvenSense Corporation, All Rights Reserved. | ||
| 4 | |||
| 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. | ||
| 9 | |||
| 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. | ||
| 14 | |||
| 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/>. | ||
| 17 | $ | ||
| 18 | */ | ||
| 19 | |||
| 20 | /** | ||
| 21 | * @addtogroup COMPASSDL | ||
| 22 | * | ||
| 23 | * @{ | ||
| 24 | * @file mmc314x.c | ||
| 25 | * @brief Magnetometer setup and handling methods for the | ||
| 26 | * MEMSIC MMC314x compass. | ||
| 27 | */ | ||
| 28 | |||
| 29 | /* -------------------------------------------------------------------------- */ | ||
| 30 | |||
| 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> | ||
| 38 | #include "mpu-dev.h" | ||
| 39 | |||
| 40 | #include <log.h> | ||
| 41 | #include <linux/mpu.h> | ||
| 42 | #include "mlsl.h" | ||
| 43 | #include "mldl_cfg.h" | ||
| 44 | #undef MPL_LOG_TAG | ||
| 45 | #define MPL_LOG_TAG "MPL-compass" | ||
| 46 | |||
| 47 | /* -------------------------------------------------------------------------- */ | ||
| 48 | |||
| 49 | static int reset_int = 1000; | ||
| 50 | static int read_count = 1; | ||
| 51 | static char reset_mode; /* in Z-init section */ | ||
| 52 | |||
| 53 | /* -------------------------------------------------------------------------- */ | ||
| 54 | #define MMC314X_REG_ST (0x00) | ||
| 55 | #define MMC314X_REG_X_MSB (0x01) | ||
| 56 | |||
| 57 | #define MMC314X_CNTL_MODE_WAKE_UP (0x01) | ||
| 58 | #define MMC314X_CNTL_MODE_SET (0x02) | ||
| 59 | #define MMC314X_CNTL_MODE_RESET (0x04) | ||
| 60 | |||
| 61 | /* -------------------------------------------------------------------------- */ | ||
| 62 | |||
| 63 | static int mmc314x_suspend(void *mlsl_handle, | ||
| 64 | struct ext_slave_descr *slave, | ||
| 65 | struct ext_slave_platform_data *pdata) | ||
| 66 | { | ||
| 67 | int result = INV_SUCCESS; | ||
| 68 | |||
| 69 | return result; | ||
| 70 | } | ||
| 71 | |||
| 72 | static int mmc314x_resume(void *mlsl_handle, | ||
| 73 | struct ext_slave_descr *slave, | ||
| 74 | struct ext_slave_platform_data *pdata) | ||
| 75 | { | ||
| 76 | |||
| 77 | int result; | ||
| 78 | result = | ||
| 79 | inv_serial_single_write(mlsl_handle, pdata->address, | ||
| 80 | MMC314X_REG_ST, MMC314X_CNTL_MODE_RESET); | ||
| 81 | if (result) { | ||
| 82 | LOG_RESULT_LOCATION(result); | ||
| 83 | return result; | ||
| 84 | } | ||
| 85 | msleep(10); | ||
| 86 | result = | ||
| 87 | inv_serial_single_write(mlsl_handle, pdata->address, | ||
| 88 | MMC314X_REG_ST, MMC314X_CNTL_MODE_SET); | ||
| 89 | if (result) { | ||
| 90 | LOG_RESULT_LOCATION(result); | ||
| 91 | return result; | ||
| 92 | } | ||
| 93 | msleep(10); | ||
| 94 | read_count = 1; | ||
| 95 | return INV_SUCCESS; | ||
| 96 | } | ||
| 97 | |||
| 98 | static int mmc314x_read(void *mlsl_handle, | ||
| 99 | struct ext_slave_descr *slave, | ||
| 100 | struct ext_slave_platform_data *pdata, | ||
| 101 | unsigned char *data) | ||
| 102 | { | ||
| 103 | int result, ii; | ||
| 104 | short tmp[3]; | ||
| 105 | unsigned char tmpdata[6]; | ||
| 106 | |||
| 107 | if (read_count > 1000) | ||
| 108 | read_count = 1; | ||
| 109 | |||
| 110 | result = | ||
| 111 | inv_serial_read(mlsl_handle, pdata->address, MMC314X_REG_X_MSB, | ||
| 112 | 6, (unsigned char *)data); | ||
| 113 | if (result) { | ||
| 114 | LOG_RESULT_LOCATION(result); | ||
| 115 | return result; | ||
| 116 | } | ||
| 117 | |||
| 118 | for (ii = 0; ii < 6; ii++) | ||
| 119 | tmpdata[ii] = data[ii]; | ||
| 120 | |||
| 121 | for (ii = 0; ii < 3; ii++) { | ||
| 122 | tmp[ii] = (short)((tmpdata[2 * ii] << 8) + tmpdata[2 * ii + 1]); | ||
| 123 | tmp[ii] = tmp[ii] - 4096; | ||
| 124 | tmp[ii] = tmp[ii] * 16; | ||
| 125 | } | ||
| 126 | |||
| 127 | for (ii = 0; ii < 3; ii++) { | ||
| 128 | data[2 * ii] = (unsigned char)(tmp[ii] >> 8); | ||
| 129 | data[2 * ii + 1] = (unsigned char)(tmp[ii]); | ||
| 130 | } | ||
| 131 | |||
| 132 | if (read_count % reset_int == 0) { | ||
| 133 | if (reset_mode) { | ||
| 134 | result = | ||
| 135 | inv_serial_single_write(mlsl_handle, | ||
| 136 | pdata->address, | ||
| 137 | MMC314X_REG_ST, | ||
| 138 | MMC314X_CNTL_MODE_RESET); | ||
| 139 | if (result) { | ||
| 140 | LOG_RESULT_LOCATION(result); | ||
| 141 | return result; | ||
| 142 | } | ||
| 143 | reset_mode = 0; | ||
| 144 | return INV_ERROR_COMPASS_DATA_NOT_READY; | ||
| 145 | } else { | ||
| 146 | result = | ||
| 147 | inv_serial_single_write(mlsl_handle, | ||
| 148 | pdata->address, | ||
| 149 | MMC314X_REG_ST, | ||
| 150 | MMC314X_CNTL_MODE_SET); | ||
| 151 | if (result) { | ||
| 152 | LOG_RESULT_LOCATION(result); | ||
| 153 | return result; | ||
| 154 | } | ||
| 155 | reset_mode = 1; | ||
| 156 | read_count++; | ||
| 157 | return INV_ERROR_COMPASS_DATA_NOT_READY; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | result = | ||
| 161 | inv_serial_single_write(mlsl_handle, pdata->address, | ||
| 162 | MMC314X_REG_ST, MMC314X_CNTL_MODE_WAKE_UP); | ||
| 163 | if (result) { | ||
| 164 | LOG_RESULT_LOCATION(result); | ||
| 165 | return result; | ||
| 166 | } | ||
| 167 | read_count++; | ||
| 168 | |||
| 169 | return INV_SUCCESS; | ||
| 170 | } | ||
| 171 | |||
| 172 | static struct ext_slave_descr mmc314x_descr = { | ||
| 173 | .init = NULL, | ||
| 174 | .exit = NULL, | ||
| 175 | .suspend = mmc314x_suspend, | ||
| 176 | .resume = mmc314x_resume, | ||
| 177 | .read = mmc314x_read, | ||
| 178 | .config = NULL, | ||
| 179 | .get_config = NULL, | ||
| 180 | .name = "mmc314x", | ||
| 181 | .type = EXT_SLAVE_TYPE_COMPASS, | ||
| 182 | .id = COMPASS_ID_MMC314X, | ||
| 183 | .read_reg = 0x01, | ||
| 184 | .read_len = 6, | ||
| 185 | .endian = EXT_SLAVE_BIG_ENDIAN, | ||
| 186 | .range = {400, 0}, | ||
| 187 | .trigger = NULL, | ||
| 188 | }; | ||
| 189 | |||
| 190 | static | ||
| 191 | struct ext_slave_descr *mmc314x_get_slave_descr(void) | ||
| 192 | { | ||
| 193 | return &mmc314x_descr; | ||
| 194 | } | ||
| 195 | |||
| 196 | /* -------------------------------------------------------------------------- */ | ||
| 197 | struct mmc314x_mod_private_data { | ||
| 198 | struct i2c_client *client; | ||
| 199 | struct ext_slave_platform_data *pdata; | ||
| 200 | }; | ||
| 201 | |||
| 202 | static unsigned short normal_i2c[] = { I2C_CLIENT_END }; | ||
| 203 | |||
| 204 | static int mmc314x_mod_probe(struct i2c_client *client, | ||
| 205 | const struct i2c_device_id *devid) | ||
| 206 | { | ||
| 207 | struct ext_slave_platform_data *pdata; | ||
| 208 | struct mmc314x_mod_private_data *private_data; | ||
| 209 | int result = 0; | ||
| 210 | |||
| 211 | dev_info(&client->adapter->dev, "%s: %s\n", __func__, devid->name); | ||
| 212 | |||
| 213 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | ||
| 214 | result = -ENODEV; | ||
| 215 | goto out_no_free; | ||
| 216 | } | ||
| 217 | |||
| 218 | pdata = client->dev.platform_data; | ||
| 219 | if (!pdata) { | ||
| 220 | dev_err(&client->adapter->dev, | ||
| 221 | "Missing platform data for slave %s\n", devid->name); | ||
| 222 | result = -EFAULT; | ||
| 223 | goto out_no_free; | ||
| 224 | } | ||
| 225 | |||
| 226 | private_data = kzalloc(sizeof(*private_data), GFP_KERNEL); | ||
| 227 | if (!private_data) { | ||
| 228 | result = -ENOMEM; | ||
| 229 | goto out_no_free; | ||
| 230 | } | ||
| 231 | |||
| 232 | i2c_set_clientdata(client, private_data); | ||
| 233 | private_data->client = client; | ||
| 234 | private_data->pdata = pdata; | ||
| 235 | |||
| 236 | result = inv_mpu_register_slave(THIS_MODULE, client, pdata, | ||
| 237 | mmc314x_get_slave_descr); | ||
| 238 | if (result) { | ||
| 239 | dev_err(&client->adapter->dev, | ||
| 240 | "Slave registration failed: %s, %d\n", | ||
| 241 | devid->name, result); | ||
| 242 | goto out_free_memory; | ||
| 243 | } | ||
| 244 | |||
| 245 | return result; | ||
| 246 | |||
| 247 | out_free_memory: | ||
| 248 | kfree(private_data); | ||
| 249 | out_no_free: | ||
| 250 | dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result); | ||
| 251 | return result; | ||
| 252 | |||
| 253 | } | ||
| 254 | |||
| 255 | static int mmc314x_mod_remove(struct i2c_client *client) | ||
| 256 | { | ||
| 257 | struct mmc314x_mod_private_data *private_data = | ||
| 258 | i2c_get_clientdata(client); | ||
| 259 | |||
| 260 | dev_dbg(&client->adapter->dev, "%s\n", __func__); | ||
| 261 | |||
| 262 | inv_mpu_unregister_slave(client, private_data->pdata, | ||
| 263 | mmc314x_get_slave_descr); | ||
| 264 | |||
| 265 | kfree(private_data); | ||
| 266 | return 0; | ||
| 267 | } | ||
| 268 | |||
| 269 | static const struct i2c_device_id mmc314x_mod_id[] = { | ||
| 270 | { "mmc314x", COMPASS_ID_MMC314X }, | ||
| 271 | {} | ||
| 272 | }; | ||
| 273 | |||
| 274 | MODULE_DEVICE_TABLE(i2c, mmc314x_mod_id); | ||
| 275 | |||
| 276 | static struct i2c_driver mmc314x_mod_driver = { | ||
| 277 | .class = I2C_CLASS_HWMON, | ||
| 278 | .probe = mmc314x_mod_probe, | ||
| 279 | .remove = mmc314x_mod_remove, | ||
| 280 | .id_table = mmc314x_mod_id, | ||
| 281 | .driver = { | ||
| 282 | .owner = THIS_MODULE, | ||
| 283 | .name = "mmc314x_mod", | ||
| 284 | }, | ||
| 285 | .address_list = normal_i2c, | ||
| 286 | }; | ||
| 287 | |||
| 288 | static int __init mmc314x_mod_init(void) | ||
| 289 | { | ||
| 290 | int res = i2c_add_driver(&mmc314x_mod_driver); | ||
| 291 | pr_info("%s: Probe name %s\n", __func__, "mmc314x_mod"); | ||
| 292 | if (res) | ||
| 293 | pr_err("%s failed\n", __func__); | ||
| 294 | return res; | ||
| 295 | } | ||
| 296 | |||
| 297 | static void __exit mmc314x_mod_exit(void) | ||
| 298 | { | ||
| 299 | pr_info("%s\n", __func__); | ||
| 300 | i2c_del_driver(&mmc314x_mod_driver); | ||
| 301 | } | ||
| 302 | |||
| 303 | module_init(mmc314x_mod_init); | ||
| 304 | module_exit(mmc314x_mod_exit); | ||
| 305 | |||
| 306 | MODULE_AUTHOR("Invensense Corporation"); | ||
| 307 | MODULE_DESCRIPTION("Driver to integrate MMC314X sensor with the MPU"); | ||
| 308 | MODULE_LICENSE("GPL"); | ||
| 309 | MODULE_ALIAS("mmc314x_mod"); | ||
| 310 | |||
| 311 | /** | ||
| 312 | * @} | ||
| 313 | */ | ||
