| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * ADLX345/346 Three-Axis Digital Accelerometers (I2C Interface) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Enter bugs at http://blackfin.uclinux.org/
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc. | 
					
						
							|  |  |  |  * Licensed under the GPL-2 or later. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <linux/input.h>	/* BUS_I2C */
 | 
					
						
							|  |  |  | #include <linux/i2c.h>
 | 
					
						
							|  |  |  | #include <linux/module.h>
 | 
					
						
							|  |  |  | #include <linux/types.h>
 | 
					
						
							| 
									
										
										
										
											2011-02-11 08:51:52 -08:00
										 |  |  | #include <linux/pm.h>
 | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | #include "adxl34x.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int adxl34x_smbus_read(struct device *dev, unsigned char reg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct i2c_client *client = to_i2c_client(dev); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return i2c_smbus_read_byte_data(client, reg); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int adxl34x_smbus_write(struct device *dev, | 
					
						
							|  |  |  | 			       unsigned char reg, unsigned char val) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct i2c_client *client = to_i2c_client(dev); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return i2c_smbus_write_byte_data(client, reg, val); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int adxl34x_smbus_read_block(struct device *dev, | 
					
						
							|  |  |  | 				    unsigned char reg, int count, | 
					
						
							|  |  |  | 				    void *buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct i2c_client *client = to_i2c_client(dev); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return i2c_smbus_read_i2c_block_data(client, reg, count, buf); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int adxl34x_i2c_read_block(struct device *dev, | 
					
						
							|  |  |  | 				  unsigned char reg, int count, | 
					
						
							|  |  |  | 				  void *buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct i2c_client *client = to_i2c_client(dev); | 
					
						
							|  |  |  | 	int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ret = i2c_master_send(client, ®, 1); | 
					
						
							|  |  |  | 	if (ret < 0) | 
					
						
							|  |  |  | 		return ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ret = i2c_master_recv(client, buf, count); | 
					
						
							|  |  |  | 	if (ret < 0) | 
					
						
							|  |  |  | 		return ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ret != count) | 
					
						
							|  |  |  | 		return -EIO; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-01 09:07:33 -07:00
										 |  |  | static const struct adxl34x_bus_ops adxl34x_smbus_bops = { | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | 	.bustype	= BUS_I2C, | 
					
						
							|  |  |  | 	.write		= adxl34x_smbus_write, | 
					
						
							|  |  |  | 	.read		= adxl34x_smbus_read, | 
					
						
							|  |  |  | 	.read_block	= adxl34x_smbus_read_block, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-01 09:07:33 -07:00
										 |  |  | static const struct adxl34x_bus_ops adxl34x_i2c_bops = { | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | 	.bustype	= BUS_I2C, | 
					
						
							|  |  |  | 	.write		= adxl34x_smbus_write, | 
					
						
							|  |  |  | 	.read		= adxl34x_smbus_read, | 
					
						
							|  |  |  | 	.read_block	= adxl34x_i2c_read_block, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-23 21:38:25 -08:00
										 |  |  | static int adxl34x_i2c_probe(struct i2c_client *client, | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | 				       const struct i2c_device_id *id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct adxl34x *ac; | 
					
						
							|  |  |  | 	int error; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	error = i2c_check_functionality(client->adapter, | 
					
						
							|  |  |  | 			I2C_FUNC_SMBUS_BYTE_DATA); | 
					
						
							|  |  |  | 	if (!error) { | 
					
						
							|  |  |  | 		dev_err(&client->dev, "SMBUS Byte Data not Supported\n"); | 
					
						
							|  |  |  | 		return -EIO; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ac = adxl34x_probe(&client->dev, client->irq, false, | 
					
						
							|  |  |  | 			   i2c_check_functionality(client->adapter, | 
					
						
							|  |  |  | 						   I2C_FUNC_SMBUS_READ_I2C_BLOCK) ? | 
					
						
							| 
									
										
										
										
											2010-07-01 09:07:33 -07:00
										 |  |  | 				&adxl34x_smbus_bops : &adxl34x_i2c_bops); | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | 	if (IS_ERR(ac)) | 
					
						
							|  |  |  | 		return PTR_ERR(ac); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	i2c_set_clientdata(client, ac); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-23 21:50:47 -08:00
										 |  |  | static int adxl34x_i2c_remove(struct i2c_client *client) | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct adxl34x *ac = i2c_get_clientdata(client); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return adxl34x_remove(ac); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-26 21:38:47 -07:00
										 |  |  | #ifdef CONFIG_PM_SLEEP
 | 
					
						
							| 
									
										
										
										
											2011-02-11 08:51:52 -08:00
										 |  |  | static int adxl34x_i2c_suspend(struct device *dev) | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-11 08:51:52 -08:00
										 |  |  | 	struct i2c_client *client = to_i2c_client(dev); | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | 	struct adxl34x *ac = i2c_get_clientdata(client); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-01 09:07:33 -07:00
										 |  |  | 	adxl34x_suspend(ac); | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-11 08:51:52 -08:00
										 |  |  | static int adxl34x_i2c_resume(struct device *dev) | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-11 08:51:52 -08:00
										 |  |  | 	struct i2c_client *client = to_i2c_client(dev); | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | 	struct adxl34x *ac = i2c_get_clientdata(client); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-01 09:07:33 -07:00
										 |  |  | 	adxl34x_resume(ac); | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-11 08:51:52 -08:00
										 |  |  | static SIMPLE_DEV_PM_OPS(adxl34x_i2c_pm, adxl34x_i2c_suspend, | 
					
						
							|  |  |  | 			 adxl34x_i2c_resume); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | static const struct i2c_device_id adxl34x_id[] = { | 
					
						
							|  |  |  | 	{ "adxl34x", 0 }, | 
					
						
							|  |  |  | 	{ } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MODULE_DEVICE_TABLE(i2c, adxl34x_id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct i2c_driver adxl34x_driver = { | 
					
						
							|  |  |  | 	.driver = { | 
					
						
							|  |  |  | 		.name = "adxl34x", | 
					
						
							|  |  |  | 		.owner = THIS_MODULE, | 
					
						
							| 
									
										
										
										
											2011-02-11 08:51:52 -08:00
										 |  |  | 		.pm = &adxl34x_i2c_pm, | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	.probe    = adxl34x_i2c_probe, | 
					
						
							| 
									
										
										
										
											2012-11-23 21:27:39 -08:00
										 |  |  | 	.remove   = adxl34x_i2c_remove, | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | 	.id_table = adxl34x_id, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-16 23:05:41 -07:00
										 |  |  | module_i2c_driver(adxl34x_driver); | 
					
						
							| 
									
										
										
										
											2010-06-25 08:44:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); | 
					
						
							|  |  |  | MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer I2C Bus Driver"); | 
					
						
							|  |  |  | MODULE_LICENSE("GPL"); |