Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * drivers/hwmon/lis3lv02d_i2c.c
  3 *
  4 * Implements I2C interface for lis3lv02d (STMicroelectronics) accelerometer.
  5 * Driver is based on corresponding SPI driver written by Daniel Mack
  6 * (lis3lv02d_spi.c (C) 2009 Daniel Mack <daniel@caiaq.de> ).
  7 *
  8 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
  9 *
 10 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
 11 *
 12 * This program is free software; you can redistribute it and/or
 13 * modify it under the terms of the GNU General Public License
 14 * version 2 as published by the Free Software Foundation.
 15 *
 16 * This program is distributed in the hope that it will be useful, but
 17 * WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * General Public License for more details.
 20 *
 21 * You should have received a copy of the GNU General Public License
 22 * along with this program; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 24 * 02110-1301 USA
 25 */
 26
 27#include <linux/module.h>
 28#include <linux/kernel.h>
 29#include <linux/init.h>
 30#include <linux/err.h>
 31#include <linux/i2c.h>
 32#include <linux/pm_runtime.h>
 33#include <linux/delay.h>
 
 
 
 
 34#include "lis3lv02d.h"
 35
 36#define DRV_NAME	"lis3lv02d_i2c"
 37
 38static const char reg_vdd[]    = "Vdd";
 39static const char reg_vdd_io[] = "Vdd_IO";
 40
 41static int lis3_reg_ctrl(struct lis3lv02d *lis3, bool state)
 42{
 43	int ret;
 44	if (state == LIS3_REG_OFF) {
 45		ret = regulator_bulk_disable(ARRAY_SIZE(lis3->regulators),
 46					lis3->regulators);
 47	} else {
 48		ret = regulator_bulk_enable(ARRAY_SIZE(lis3->regulators),
 49					lis3->regulators);
 50		/* Chip needs time to wakeup. Not mentioned in datasheet */
 51		usleep_range(10000, 20000);
 52	}
 53	return ret;
 54}
 55
 56static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
 57{
 58	struct i2c_client *c = lis3->bus_priv;
 59	return i2c_smbus_write_byte_data(c, reg, value);
 60}
 61
 62static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
 63{
 64	struct i2c_client *c = lis3->bus_priv;
 65	*v = i2c_smbus_read_byte_data(c, reg);
 66	return 0;
 67}
 68
 69static inline s32 lis3_i2c_blockread(struct lis3lv02d *lis3, int reg, int len,
 70				u8 *v)
 71{
 72	struct i2c_client *c = lis3->bus_priv;
 73	reg |= (1 << 7); /* 7th bit enables address auto incrementation */
 74	return i2c_smbus_read_i2c_block_data(c, reg, len, v);
 75}
 76
 77static int lis3_i2c_init(struct lis3lv02d *lis3)
 78{
 79	u8 reg;
 80	int ret;
 81
 82	if (lis3->reg_ctrl)
 83		lis3_reg_ctrl(lis3, LIS3_REG_ON);
 84
 85	lis3->read(lis3, WHO_AM_I, &reg);
 86	if (reg != lis3->whoami)
 87		printk(KERN_ERR "lis3: power on failure\n");
 88
 89	/* power up the device */
 90	ret = lis3->read(lis3, CTRL_REG1, &reg);
 91	if (ret < 0)
 92		return ret;
 93
 94	reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
 
 
 
 
 95	return lis3->write(lis3, CTRL_REG1, reg);
 96}
 97
 98/* Default axis mapping but it can be overwritten by platform data */
 99static union axis_conversion lis3lv02d_axis_map =
100	{ .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
101
102static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
103					const struct i2c_device_id *id)
 
 
 
 
 
 
 
104{
105	int ret = 0;
106	struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
107
108	if (pdata) {
109		/* Regulator control is optional */
110		if (pdata->driver_features & LIS3_USE_REGULATOR_CTRL)
111			lis3_dev.reg_ctrl = lis3_reg_ctrl;
 
 
 
 
 
112
 
113		if ((pdata->driver_features & LIS3_USE_BLOCK_READ) &&
114			(i2c_check_functionality(client->adapter,
115						I2C_FUNC_SMBUS_I2C_BLOCK)))
116			lis3_dev.blkread  = lis3_i2c_blockread;
117
118		if (pdata->axis_x)
119			lis3lv02d_axis_map.x = pdata->axis_x;
120
121		if (pdata->axis_y)
122			lis3lv02d_axis_map.y = pdata->axis_y;
123
124		if (pdata->axis_z)
125			lis3lv02d_axis_map.z = pdata->axis_z;
126
127		if (pdata->setup_resources)
128			ret = pdata->setup_resources();
129
130		if (ret)
131			goto fail;
132	}
133
134	if (lis3_dev.reg_ctrl) {
135		lis3_dev.regulators[0].supply = reg_vdd;
136		lis3_dev.regulators[1].supply = reg_vdd_io;
137		ret = regulator_bulk_get(&client->dev,
138					ARRAY_SIZE(lis3_dev.regulators),
139					lis3_dev.regulators);
140		if (ret < 0)
141			goto fail;
142	}
143
144	lis3_dev.pdata	  = pdata;
145	lis3_dev.bus_priv = client;
146	lis3_dev.init	  = lis3_i2c_init;
147	lis3_dev.read	  = lis3_i2c_read;
148	lis3_dev.write	  = lis3_i2c_write;
149	lis3_dev.irq	  = client->irq;
150	lis3_dev.ac	  = lis3lv02d_axis_map;
151	lis3_dev.pm_dev	  = &client->dev;
152
153	i2c_set_clientdata(client, &lis3_dev);
154
155	/* Provide power over the init call */
156	if (lis3_dev.reg_ctrl)
157		lis3_reg_ctrl(&lis3_dev, LIS3_REG_ON);
158
159	ret = lis3lv02d_init_device(&lis3_dev);
160
161	if (lis3_dev.reg_ctrl)
162		lis3_reg_ctrl(&lis3_dev, LIS3_REG_OFF);
 
 
 
163
164	if (ret == 0)
165		return 0;
 
166fail:
167	if (pdata && pdata->release_resources)
168		pdata->release_resources();
169	return ret;
170}
171
172static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client)
173{
174	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
175	struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
176
177	if (pdata && pdata->release_resources)
178		pdata->release_resources();
179
180	lis3lv02d_joystick_disable();
181	lis3lv02d_remove_fs(&lis3_dev);
182
183	if (lis3_dev.reg_ctrl)
184		regulator_bulk_free(ARRAY_SIZE(lis3->regulators),
185				lis3_dev.regulators);
186	return 0;
187}
188
189#ifdef CONFIG_PM_SLEEP
190static int lis3lv02d_i2c_suspend(struct device *dev)
191{
192	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
193	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
194
195	if (!lis3->pdata || !lis3->pdata->wakeup_flags)
196		lis3lv02d_poweroff(lis3);
197	return 0;
198}
199
200static int lis3lv02d_i2c_resume(struct device *dev)
201{
202	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
203	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
204
205	/*
206	 * pm_runtime documentation says that devices should always
207	 * be powered on at resume. Pm_runtime turns them off after system
208	 * wide resume is complete.
209	 */
210	if (!lis3->pdata || !lis3->pdata->wakeup_flags ||
211		pm_runtime_suspended(dev))
212		lis3lv02d_poweron(lis3);
213
214	return 0;
215}
216#endif /* CONFIG_PM_SLEEP */
217
218#ifdef CONFIG_PM_RUNTIME
219static int lis3_i2c_runtime_suspend(struct device *dev)
220{
221	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
222	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
223
224	lis3lv02d_poweroff(lis3);
225	return 0;
226}
227
228static int lis3_i2c_runtime_resume(struct device *dev)
229{
230	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
231	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
232
233	lis3lv02d_poweron(lis3);
234	return 0;
235}
236#endif /* CONFIG_PM_RUNTIME */
237
238static const struct i2c_device_id lis3lv02d_id[] = {
239	{"lis3lv02d", 0 },
 
240	{}
241};
242
243MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
244
245static const struct dev_pm_ops lis3_pm_ops = {
246	SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend,
247				lis3lv02d_i2c_resume)
248	SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend,
249			   lis3_i2c_runtime_resume,
250			   NULL)
251};
252
253static struct i2c_driver lis3lv02d_i2c_driver = {
254	.driver	 = {
255		.name   = DRV_NAME,
256		.owner  = THIS_MODULE,
257		.pm     = &lis3_pm_ops,
 
258	},
259	.probe	= lis3lv02d_i2c_probe,
260	.remove	= __devexit_p(lis3lv02d_i2c_remove),
261	.id_table = lis3lv02d_id,
262};
263
264static int __init lis3lv02d_init(void)
265{
266	return i2c_add_driver(&lis3lv02d_i2c_driver);
267}
268
269static void __exit lis3lv02d_exit(void)
270{
271	i2c_del_driver(&lis3lv02d_i2c_driver);
272}
273
274MODULE_AUTHOR("Nokia Corporation");
275MODULE_DESCRIPTION("lis3lv02d I2C interface");
276MODULE_LICENSE("GPL");
277
278module_init(lis3lv02d_init);
279module_exit(lis3lv02d_exit);
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * drivers/hwmon/lis3lv02d_i2c.c
  4 *
  5 * Implements I2C interface for lis3lv02d (STMicroelectronics) accelerometer.
  6 * Driver is based on corresponding SPI driver written by Daniel Mack
  7 * (lis3lv02d_spi.c (C) 2009 Daniel Mack <daniel@caiaq.de> ).
  8 *
  9 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 10 *
 11 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 12 */
 13
 14#include <linux/module.h>
 15#include <linux/kernel.h>
 
 16#include <linux/err.h>
 17#include <linux/i2c.h>
 18#include <linux/pm_runtime.h>
 19#include <linux/delay.h>
 20#include <linux/of.h>
 21#include <linux/of_platform.h>
 22#include <linux/of_device.h>
 23
 24#include "lis3lv02d.h"
 25
 26#define DRV_NAME	"lis3lv02d_i2c"
 27
 28static const char reg_vdd[]    = "Vdd";
 29static const char reg_vdd_io[] = "Vdd_IO";
 30
 31static int lis3_reg_ctrl(struct lis3lv02d *lis3, bool state)
 32{
 33	int ret;
 34	if (state == LIS3_REG_OFF) {
 35		ret = regulator_bulk_disable(ARRAY_SIZE(lis3->regulators),
 36					lis3->regulators);
 37	} else {
 38		ret = regulator_bulk_enable(ARRAY_SIZE(lis3->regulators),
 39					lis3->regulators);
 40		/* Chip needs time to wakeup. Not mentioned in datasheet */
 41		usleep_range(10000, 20000);
 42	}
 43	return ret;
 44}
 45
 46static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
 47{
 48	struct i2c_client *c = lis3->bus_priv;
 49	return i2c_smbus_write_byte_data(c, reg, value);
 50}
 51
 52static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
 53{
 54	struct i2c_client *c = lis3->bus_priv;
 55	*v = i2c_smbus_read_byte_data(c, reg);
 56	return 0;
 57}
 58
 59static inline s32 lis3_i2c_blockread(struct lis3lv02d *lis3, int reg, int len,
 60				u8 *v)
 61{
 62	struct i2c_client *c = lis3->bus_priv;
 63	reg |= (1 << 7); /* 7th bit enables address auto incrementation */
 64	return i2c_smbus_read_i2c_block_data(c, reg, len, v);
 65}
 66
 67static int lis3_i2c_init(struct lis3lv02d *lis3)
 68{
 69	u8 reg;
 70	int ret;
 71
 72	lis3_reg_ctrl(lis3, LIS3_REG_ON);
 
 73
 74	lis3->read(lis3, WHO_AM_I, &reg);
 75	if (reg != lis3->whoami)
 76		printk(KERN_ERR "lis3: power on failure\n");
 77
 78	/* power up the device */
 79	ret = lis3->read(lis3, CTRL_REG1, &reg);
 80	if (ret < 0)
 81		return ret;
 82
 83	if (lis3->whoami == WAI_3DLH)
 84		reg |= CTRL1_PM0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
 85	else
 86		reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
 87
 88	return lis3->write(lis3, CTRL_REG1, reg);
 89}
 90
 91/* Default axis mapping but it can be overwritten by platform data */
 92static union axis_conversion lis3lv02d_axis_map =
 93	{ .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
 94
 95#ifdef CONFIG_OF
 96static const struct of_device_id lis3lv02d_i2c_dt_ids[] = {
 97	{ .compatible = "st,lis3lv02d" },
 98	{}
 99};
100MODULE_DEVICE_TABLE(of, lis3lv02d_i2c_dt_ids);
101#endif
102
103static int lis3lv02d_i2c_probe(struct i2c_client *client)
104{
105	int ret = 0;
106	struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
107
108#ifdef CONFIG_OF
109	if (of_match_device(lis3lv02d_i2c_dt_ids, &client->dev)) {
110		lis3_dev.of_node = client->dev.of_node;
111		ret = lis3lv02d_init_dt(&lis3_dev);
112		if (ret)
113			return ret;
114		pdata = lis3_dev.pdata;
115	}
116#endif
117
118	if (pdata) {
119		if ((pdata->driver_features & LIS3_USE_BLOCK_READ) &&
120			(i2c_check_functionality(client->adapter,
121						I2C_FUNC_SMBUS_I2C_BLOCK)))
122			lis3_dev.blkread  = lis3_i2c_blockread;
123
124		if (pdata->axis_x)
125			lis3lv02d_axis_map.x = pdata->axis_x;
126
127		if (pdata->axis_y)
128			lis3lv02d_axis_map.y = pdata->axis_y;
129
130		if (pdata->axis_z)
131			lis3lv02d_axis_map.z = pdata->axis_z;
132
133		if (pdata->setup_resources)
134			ret = pdata->setup_resources();
135
136		if (ret)
137			goto fail;
138	}
139
140	lis3_dev.regulators[0].supply = reg_vdd;
141	lis3_dev.regulators[1].supply = reg_vdd_io;
142	ret = regulator_bulk_get(&client->dev,
143				 ARRAY_SIZE(lis3_dev.regulators),
144				 lis3_dev.regulators);
145	if (ret < 0)
146		goto fail;
 
 
147
148	lis3_dev.pdata	  = pdata;
149	lis3_dev.bus_priv = client;
150	lis3_dev.init	  = lis3_i2c_init;
151	lis3_dev.read	  = lis3_i2c_read;
152	lis3_dev.write	  = lis3_i2c_write;
153	lis3_dev.irq	  = client->irq;
154	lis3_dev.ac	  = lis3lv02d_axis_map;
155	lis3_dev.pm_dev	  = &client->dev;
156
157	i2c_set_clientdata(client, &lis3_dev);
158
159	/* Provide power over the init call */
160	lis3_reg_ctrl(&lis3_dev, LIS3_REG_ON);
 
161
162	ret = lis3lv02d_init_device(&lis3_dev);
163
164	lis3_reg_ctrl(&lis3_dev, LIS3_REG_OFF);
165
166	if (ret)
167		goto fail2;
168	return 0;
169
170fail2:
171	regulator_bulk_free(ARRAY_SIZE(lis3_dev.regulators),
172				lis3_dev.regulators);
173fail:
174	if (pdata && pdata->release_resources)
175		pdata->release_resources();
176	return ret;
177}
178
179static void lis3lv02d_i2c_remove(struct i2c_client *client)
180{
181	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
182	struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
183
184	if (pdata && pdata->release_resources)
185		pdata->release_resources();
186
187	lis3lv02d_joystick_disable(lis3);
188	lis3lv02d_remove_fs(&lis3_dev);
189
190	regulator_bulk_free(ARRAY_SIZE(lis3->regulators),
191			    lis3_dev.regulators);
 
 
192}
193
194#ifdef CONFIG_PM_SLEEP
195static int lis3lv02d_i2c_suspend(struct device *dev)
196{
197	struct i2c_client *client = to_i2c_client(dev);
198	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
199
200	if (!lis3->pdata || !lis3->pdata->wakeup_flags)
201		lis3lv02d_poweroff(lis3);
202	return 0;
203}
204
205static int lis3lv02d_i2c_resume(struct device *dev)
206{
207	struct i2c_client *client = to_i2c_client(dev);
208	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
209
210	/*
211	 * pm_runtime documentation says that devices should always
212	 * be powered on at resume. Pm_runtime turns them off after system
213	 * wide resume is complete.
214	 */
215	if (!lis3->pdata || !lis3->pdata->wakeup_flags ||
216		pm_runtime_suspended(dev))
217		lis3lv02d_poweron(lis3);
218
219	return 0;
220}
221#endif /* CONFIG_PM_SLEEP */
222
223#ifdef CONFIG_PM
224static int lis3_i2c_runtime_suspend(struct device *dev)
225{
226	struct i2c_client *client = to_i2c_client(dev);
227	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
228
229	lis3lv02d_poweroff(lis3);
230	return 0;
231}
232
233static int lis3_i2c_runtime_resume(struct device *dev)
234{
235	struct i2c_client *client = to_i2c_client(dev);
236	struct lis3lv02d *lis3 = i2c_get_clientdata(client);
237
238	lis3lv02d_poweron(lis3);
239	return 0;
240}
241#endif /* CONFIG_PM */
242
243static const struct i2c_device_id lis3lv02d_id[] = {
244	{"lis3lv02d", LIS3LV02D},
245	{"lis331dlh", LIS331DLH},
246	{}
247};
248
249MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
250
251static const struct dev_pm_ops lis3_pm_ops = {
252	SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend,
253				lis3lv02d_i2c_resume)
254	SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend,
255			   lis3_i2c_runtime_resume,
256			   NULL)
257};
258
259static struct i2c_driver lis3lv02d_i2c_driver = {
260	.driver	 = {
261		.name   = DRV_NAME,
 
262		.pm     = &lis3_pm_ops,
263		.of_match_table = of_match_ptr(lis3lv02d_i2c_dt_ids),
264	},
265	.probe_new = lis3lv02d_i2c_probe,
266	.remove	= lis3lv02d_i2c_remove,
267	.id_table = lis3lv02d_id,
268};
269
270module_i2c_driver(lis3lv02d_i2c_driver);
 
 
 
 
 
 
 
 
271
272MODULE_AUTHOR("Nokia Corporation");
273MODULE_DESCRIPTION("lis3lv02d I2C interface");
274MODULE_LICENSE("GPL");