lis3lv02d_i2c.c 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * drivers/hwmon/lis3lv02d_i2c.c
 *
 * Implements I2C interface for lis3lv02d (STMicroelectronics) accelerometer.
 * Driver is based on corresponding SPI driver written by Daniel Mack
 * (lis3lv02d_spi.c (C) 2009 Daniel Mack <daniel@caiaq.de> ).
 *
 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/i2c.h>
S
Samu Onkalo 已提交
32
#include <linux/pm_runtime.h>
S
Samu Onkalo 已提交
33
#include <linux/delay.h>
34 35 36 37
#include "lis3lv02d.h"

#define DRV_NAME 	"lis3lv02d_i2c"

S
Samu Onkalo 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
static const char reg_vdd[]    = "Vdd";
static const char reg_vdd_io[] = "Vdd_IO";

static int lis3_reg_ctrl(struct lis3lv02d *lis3, bool state)
{
	int ret;
	if (state == LIS3_REG_OFF) {
		ret = regulator_bulk_disable(ARRAY_SIZE(lis3->regulators),
					lis3->regulators);
	} else {
		ret = regulator_bulk_enable(ARRAY_SIZE(lis3->regulators),
					lis3->regulators);
		/* Chip needs time to wakeup. Not mentioned in datasheet */
		usleep_range(10000, 20000);
	}
	return ret;
}

56 57 58 59 60 61 62 63 64 65 66 67 68
static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
{
	struct i2c_client *c = lis3->bus_priv;
	return i2c_smbus_write_byte_data(c, reg, value);
}

static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
{
	struct i2c_client *c = lis3->bus_priv;
	*v = i2c_smbus_read_byte_data(c, reg);
	return 0;
}

69 70 71 72 73 74 75 76
static inline s32 lis3_i2c_blockread(struct lis3lv02d *lis3, int reg, int len,
				u8 *v)
{
	struct i2c_client *c = lis3->bus_priv;
	reg |= (1 << 7); /* 7th bit enables address auto incrementation */
	return i2c_smbus_read_i2c_block_data(c, reg, len, v);
}

77 78 79 80 81
static int lis3_i2c_init(struct lis3lv02d *lis3)
{
	u8 reg;
	int ret;

S
Samu Onkalo 已提交
82 83 84 85 86 87 88
	if (lis3->reg_ctrl)
		lis3_reg_ctrl(lis3, LIS3_REG_ON);

	lis3->read(lis3, WHO_AM_I, &reg);
	if (reg != lis3->whoami)
		printk(KERN_ERR "lis3: power on failure\n");

89 90 91 92 93
	/* power up the device */
	ret = lis3->read(lis3, CTRL_REG1, &reg);
	if (ret < 0)
		return ret;

94
	reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
95 96 97 98
	return lis3->write(lis3, CTRL_REG1, reg);
}

/* Default axis mapping but it can be overwritten by platform data */
99 100
static union axis_conversion lis3lv02d_axis_map =
	{ .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
101 102 103 104 105 106 107 108

static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
					const struct i2c_device_id *id)
{
	int ret = 0;
	struct lis3lv02d_platform_data *pdata = client->dev.platform_data;

	if (pdata) {
S
Samu Onkalo 已提交
109 110 111 112
		/* Regulator control is optional */
		if (pdata->driver_features & LIS3_USE_REGULATOR_CTRL)
			lis3_dev.reg_ctrl = lis3_reg_ctrl;

113 114 115 116 117
		if ((pdata->driver_features & LIS3_USE_BLOCK_READ) &&
			(i2c_check_functionality(client->adapter,
						I2C_FUNC_SMBUS_I2C_BLOCK)))
			lis3_dev.blkread  = lis3_i2c_blockread;

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		if (pdata->axis_x)
			lis3lv02d_axis_map.x = pdata->axis_x;

		if (pdata->axis_y)
			lis3lv02d_axis_map.y = pdata->axis_y;

		if (pdata->axis_z)
			lis3lv02d_axis_map.z = pdata->axis_z;

		if (pdata->setup_resources)
			ret = pdata->setup_resources();

		if (ret)
			goto fail;
	}

S
Samu Onkalo 已提交
134 135 136 137 138 139 140 141 142 143
	if (lis3_dev.reg_ctrl) {
		lis3_dev.regulators[0].supply = reg_vdd;
		lis3_dev.regulators[1].supply = reg_vdd_io;
		ret = regulator_bulk_get(&client->dev,
					ARRAY_SIZE(lis3_dev.regulators),
					lis3_dev.regulators);
		if (ret < 0)
			goto fail;
	}

144 145 146 147 148 149 150
	lis3_dev.pdata	  = pdata;
	lis3_dev.bus_priv = client;
	lis3_dev.init	  = lis3_i2c_init;
	lis3_dev.read	  = lis3_i2c_read;
	lis3_dev.write	  = lis3_i2c_write;
	lis3_dev.irq	  = client->irq;
	lis3_dev.ac	  = lis3lv02d_axis_map;
S
Samu Onkalo 已提交
151
	lis3_dev.pm_dev	  = &client->dev;
152 153

	i2c_set_clientdata(client, &lis3_dev);
S
Samu Onkalo 已提交
154 155 156 157 158

	/* Provide power over the init call */
	if (lis3_dev.reg_ctrl)
		lis3_reg_ctrl(&lis3_dev, LIS3_REG_ON);

159
	ret = lis3lv02d_init_device(&lis3_dev);
S
Samu Onkalo 已提交
160 161 162

	if (lis3_dev.reg_ctrl)
		lis3_reg_ctrl(&lis3_dev, LIS3_REG_OFF);
163 164 165

	if (ret == 0)
		return 0;
166
fail:
167 168
	if (pdata && pdata->release_resources)
		pdata->release_resources();
169 170 171 172 173
	return ret;
}

static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client)
{
S
Samu Onkalo 已提交
174
	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
175 176 177 178 179 180
	struct lis3lv02d_platform_data *pdata = client->dev.platform_data;

	if (pdata && pdata->release_resources)
		pdata->release_resources();

	lis3lv02d_joystick_disable();
S
Samu Onkalo 已提交
181
	lis3lv02d_remove_fs(&lis3_dev);
182

S
Samu Onkalo 已提交
183 184 185 186
	if (lis3_dev.reg_ctrl)
		regulator_bulk_free(ARRAY_SIZE(lis3->regulators),
				lis3_dev.regulators);
	return 0;
187 188 189
}

#ifdef CONFIG_PM
S
Samu Onkalo 已提交
190
static int lis3lv02d_i2c_suspend(struct device *dev)
191
{
S
Samu Onkalo 已提交
192
	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
193 194
	struct lis3lv02d *lis3 = i2c_get_clientdata(client);

195
	if (!lis3->pdata || !lis3->pdata->wakeup_flags)
196 197 198 199
		lis3lv02d_poweroff(lis3);
	return 0;
}

S
Samu Onkalo 已提交
200
static int lis3lv02d_i2c_resume(struct device *dev)
201
{
S
Samu Onkalo 已提交
202
	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
203 204
	struct lis3lv02d *lis3 = i2c_get_clientdata(client);

S
Samu Onkalo 已提交
205 206 207 208 209 210 211
	/*
	 * pm_runtime documentation says that devices should always
	 * be powered on at resume. Pm_runtime turns them off after system
	 * wide resume is complete.
	 */
	if (!lis3->pdata || !lis3->pdata->wakeup_flags ||
		pm_runtime_suspended(dev))
212 213
		lis3lv02d_poweron(lis3);

S
Samu Onkalo 已提交
214
	return 0;
215 216 217 218 219 220 221
}
#else
#define lis3lv02d_i2c_suspend	NULL
#define lis3lv02d_i2c_resume	NULL
#define lis3lv02d_i2c_shutdown	NULL
#endif

S
Samu Onkalo 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static int lis3_i2c_runtime_suspend(struct device *dev)
{
	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
	struct lis3lv02d *lis3 = i2c_get_clientdata(client);

	lis3lv02d_poweroff(lis3);
	return 0;
}

static int lis3_i2c_runtime_resume(struct device *dev)
{
	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
	struct lis3lv02d *lis3 = i2c_get_clientdata(client);

	lis3lv02d_poweron(lis3);
	return 0;
}

240 241 242 243 244 245 246
static const struct i2c_device_id lis3lv02d_id[] = {
	{"lis3lv02d", 0 },
	{}
};

MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);

S
Samu Onkalo 已提交
247 248 249 250 251 252 253 254
static const struct dev_pm_ops lis3_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend,
				lis3lv02d_i2c_resume)
	SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend,
			   lis3_i2c_runtime_resume,
			   NULL)
};

255 256 257 258
static struct i2c_driver lis3lv02d_i2c_driver = {
	.driver	 = {
		.name   = DRV_NAME,
		.owner  = THIS_MODULE,
S
Samu Onkalo 已提交
259
		.pm     = &lis3_pm_ops,
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
	},
	.probe	= lis3lv02d_i2c_probe,
	.remove	= __devexit_p(lis3lv02d_i2c_remove),
	.id_table = lis3lv02d_id,
};

static int __init lis3lv02d_init(void)
{
	return i2c_add_driver(&lis3lv02d_i2c_driver);
}

static void __exit lis3lv02d_exit(void)
{
	i2c_del_driver(&lis3lv02d_i2c_driver);
}

MODULE_AUTHOR("Nokia Corporation");
MODULE_DESCRIPTION("lis3lv02d I2C interface");
MODULE_LICENSE("GPL");

module_init(lis3lv02d_init);
module_exit(lis3lv02d_exit);