0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/i2c.h>
0015 #include <linux/err.h>
0016 #include <linux/delay.h>
0017 #include <linux/mutex.h>
0018 #include <linux/sysfs.h>
0019 #include <linux/pm_runtime.h>
0020
0021 #define ALS_MIN_RANGE_VAL 1
0022 #define ALS_MAX_RANGE_VAL 2
0023 #define POWER_STA_ENABLE 1
0024 #define POWER_STA_DISABLE 0
0025
0026 #define DRIVER_NAME "apds9802als"
0027
0028 struct als_data {
0029 struct mutex mutex;
0030 };
0031
0032 static ssize_t als_sensing_range_show(struct device *dev,
0033 struct device_attribute *attr, char *buf)
0034 {
0035 struct i2c_client *client = to_i2c_client(dev);
0036 int val;
0037
0038 val = i2c_smbus_read_byte_data(client, 0x81);
0039 if (val < 0)
0040 return val;
0041 if (val & 1)
0042 return sprintf(buf, "4095\n");
0043 else
0044 return sprintf(buf, "65535\n");
0045 }
0046
0047 static int als_wait_for_data_ready(struct device *dev)
0048 {
0049 struct i2c_client *client = to_i2c_client(dev);
0050 int ret;
0051 int retry = 10;
0052
0053 do {
0054 msleep(30);
0055 ret = i2c_smbus_read_byte_data(client, 0x86);
0056 } while (!(ret & 0x80) && retry--);
0057
0058 if (retry < 0) {
0059 dev_warn(dev, "timeout waiting for data ready\n");
0060 return -ETIMEDOUT;
0061 }
0062
0063 return 0;
0064 }
0065
0066 static ssize_t als_lux0_input_data_show(struct device *dev,
0067 struct device_attribute *attr, char *buf)
0068 {
0069 struct i2c_client *client = to_i2c_client(dev);
0070 struct als_data *data = i2c_get_clientdata(client);
0071 int ret_val;
0072 int temp;
0073
0074
0075 pm_runtime_get_sync(dev);
0076 mutex_lock(&data->mutex);
0077
0078
0079 i2c_smbus_write_byte(client, 0x40);
0080
0081 temp = i2c_smbus_read_byte_data(client, 0x81);
0082 i2c_smbus_write_byte_data(client, 0x81, temp | 0x08);
0083
0084 ret_val = als_wait_for_data_ready(dev);
0085 if (ret_val < 0)
0086 goto failed;
0087
0088 temp = i2c_smbus_read_byte_data(client, 0x8C);
0089 if (temp < 0) {
0090 ret_val = temp;
0091 goto failed;
0092 }
0093 ret_val = i2c_smbus_read_byte_data(client, 0x8D);
0094 if (ret_val < 0)
0095 goto failed;
0096
0097 mutex_unlock(&data->mutex);
0098 pm_runtime_put_sync(dev);
0099
0100 temp = (ret_val << 8) | temp;
0101 return sprintf(buf, "%d\n", temp);
0102 failed:
0103 mutex_unlock(&data->mutex);
0104 pm_runtime_put_sync(dev);
0105 return ret_val;
0106 }
0107
0108 static ssize_t als_sensing_range_store(struct device *dev,
0109 struct device_attribute *attr, const char *buf, size_t count)
0110 {
0111 struct i2c_client *client = to_i2c_client(dev);
0112 struct als_data *data = i2c_get_clientdata(client);
0113 int ret_val;
0114 unsigned long val;
0115
0116 ret_val = kstrtoul(buf, 10, &val);
0117 if (ret_val)
0118 return ret_val;
0119
0120 if (val < 4096)
0121 val = 1;
0122 else if (val < 65536)
0123 val = 2;
0124 else
0125 return -ERANGE;
0126
0127 pm_runtime_get_sync(dev);
0128
0129
0130
0131 mutex_lock(&data->mutex);
0132
0133 ret_val = i2c_smbus_read_byte_data(client, 0x81);
0134 if (ret_val < 0)
0135 goto fail;
0136
0137
0138 ret_val = ret_val & 0xFA;
0139
0140 if (val == 1)
0141 ret_val = (ret_val | 0x01);
0142 else
0143 ret_val = (ret_val | 0x00);
0144
0145 ret_val = i2c_smbus_write_byte_data(client, 0x81, ret_val);
0146
0147 if (ret_val >= 0) {
0148
0149 mutex_unlock(&data->mutex);
0150 pm_runtime_put_sync(dev);
0151 return count;
0152 }
0153 fail:
0154 mutex_unlock(&data->mutex);
0155 pm_runtime_put_sync(dev);
0156 return ret_val;
0157 }
0158
0159 static int als_set_power_state(struct i2c_client *client, bool on_off)
0160 {
0161 int ret_val;
0162 struct als_data *data = i2c_get_clientdata(client);
0163
0164 mutex_lock(&data->mutex);
0165 ret_val = i2c_smbus_read_byte_data(client, 0x80);
0166 if (ret_val < 0)
0167 goto fail;
0168 if (on_off)
0169 ret_val = ret_val | 0x01;
0170 else
0171 ret_val = ret_val & 0xFE;
0172 ret_val = i2c_smbus_write_byte_data(client, 0x80, ret_val);
0173 fail:
0174 mutex_unlock(&data->mutex);
0175 return ret_val;
0176 }
0177
0178 static DEVICE_ATTR(lux0_sensor_range, S_IRUGO | S_IWUSR,
0179 als_sensing_range_show, als_sensing_range_store);
0180 static DEVICE_ATTR(lux0_input, S_IRUGO, als_lux0_input_data_show, NULL);
0181
0182 static struct attribute *mid_att_als[] = {
0183 &dev_attr_lux0_sensor_range.attr,
0184 &dev_attr_lux0_input.attr,
0185 NULL
0186 };
0187
0188 static const struct attribute_group m_als_gr = {
0189 .name = "apds9802als",
0190 .attrs = mid_att_als
0191 };
0192
0193 static int als_set_default_config(struct i2c_client *client)
0194 {
0195 int ret_val;
0196
0197 ret_val = i2c_smbus_write_byte_data(client, 0x80, 0x01);
0198 if (ret_val < 0) {
0199 dev_err(&client->dev, "failed default switch on write\n");
0200 return ret_val;
0201 }
0202
0203 ret_val = i2c_smbus_write_byte_data(client, 0x81, 0x08);
0204 if (ret_val < 0)
0205 dev_err(&client->dev, "failed default LUX on write\n");
0206
0207
0208
0209
0210 als_wait_for_data_ready(&client->dev);
0211
0212 return ret_val;
0213 }
0214
0215 static int apds9802als_probe(struct i2c_client *client,
0216 const struct i2c_device_id *id)
0217 {
0218 int res;
0219 struct als_data *data;
0220
0221 data = kzalloc(sizeof(struct als_data), GFP_KERNEL);
0222 if (data == NULL) {
0223 dev_err(&client->dev, "Memory allocation failed\n");
0224 return -ENOMEM;
0225 }
0226 i2c_set_clientdata(client, data);
0227 res = sysfs_create_group(&client->dev.kobj, &m_als_gr);
0228 if (res) {
0229 dev_err(&client->dev, "device create file failed\n");
0230 goto als_error1;
0231 }
0232 dev_info(&client->dev, "ALS chip found\n");
0233 als_set_default_config(client);
0234 mutex_init(&data->mutex);
0235
0236 pm_runtime_set_active(&client->dev);
0237 pm_runtime_enable(&client->dev);
0238
0239 return res;
0240 als_error1:
0241 kfree(data);
0242 return res;
0243 }
0244
0245 static int apds9802als_remove(struct i2c_client *client)
0246 {
0247 struct als_data *data = i2c_get_clientdata(client);
0248
0249 pm_runtime_get_sync(&client->dev);
0250
0251 als_set_power_state(client, false);
0252 sysfs_remove_group(&client->dev.kobj, &m_als_gr);
0253
0254 pm_runtime_disable(&client->dev);
0255 pm_runtime_set_suspended(&client->dev);
0256 pm_runtime_put_noidle(&client->dev);
0257
0258 kfree(data);
0259 return 0;
0260 }
0261
0262 #ifdef CONFIG_PM
0263
0264 static int apds9802als_suspend(struct device *dev)
0265 {
0266 struct i2c_client *client = to_i2c_client(dev);
0267
0268 als_set_power_state(client, false);
0269 return 0;
0270 }
0271
0272 static int apds9802als_resume(struct device *dev)
0273 {
0274 struct i2c_client *client = to_i2c_client(dev);
0275
0276 als_set_power_state(client, true);
0277 return 0;
0278 }
0279
0280 static UNIVERSAL_DEV_PM_OPS(apds9802als_pm_ops, apds9802als_suspend,
0281 apds9802als_resume, NULL);
0282
0283 #define APDS9802ALS_PM_OPS (&apds9802als_pm_ops)
0284
0285 #else
0286 #define APDS9802ALS_PM_OPS NULL
0287 #endif
0288
0289 static const struct i2c_device_id apds9802als_id[] = {
0290 { DRIVER_NAME, 0 },
0291 { }
0292 };
0293
0294 MODULE_DEVICE_TABLE(i2c, apds9802als_id);
0295
0296 static struct i2c_driver apds9802als_driver = {
0297 .driver = {
0298 .name = DRIVER_NAME,
0299 .pm = APDS9802ALS_PM_OPS,
0300 },
0301 .probe = apds9802als_probe,
0302 .remove = apds9802als_remove,
0303 .id_table = apds9802als_id,
0304 };
0305
0306 module_i2c_driver(apds9802als_driver);
0307
0308 MODULE_AUTHOR("Anantha Narayanan <Anantha.Narayanan@intel.com");
0309 MODULE_DESCRIPTION("Avago apds9802als ALS Driver");
0310 MODULE_LICENSE("GPL v2");