18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * isl29003.c - Linux kernel module for 48c2ecf20Sopenharmony_ci * Intersil ISL29003 ambient light sensor 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * See file:Documentation/misc-devices/isl29003.rst 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de> 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Based on code written by 118c2ecf20Sopenharmony_ci * Rodolfo Giometti <giometti@linux.it> 128c2ecf20Sopenharmony_ci * Eurotech S.p.A. <info@eurotech.it> 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <linux/module.h> 168c2ecf20Sopenharmony_ci#include <linux/slab.h> 178c2ecf20Sopenharmony_ci#include <linux/i2c.h> 188c2ecf20Sopenharmony_ci#include <linux/mutex.h> 198c2ecf20Sopenharmony_ci#include <linux/delay.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#define ISL29003_DRV_NAME "isl29003" 228c2ecf20Sopenharmony_ci#define DRIVER_VERSION "1.0" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define ISL29003_REG_COMMAND 0x00 258c2ecf20Sopenharmony_ci#define ISL29003_ADC_ENABLED (1 << 7) 268c2ecf20Sopenharmony_ci#define ISL29003_ADC_PD (1 << 6) 278c2ecf20Sopenharmony_ci#define ISL29003_TIMING_INT (1 << 5) 288c2ecf20Sopenharmony_ci#define ISL29003_MODE_SHIFT (2) 298c2ecf20Sopenharmony_ci#define ISL29003_MODE_MASK (0x3 << ISL29003_MODE_SHIFT) 308c2ecf20Sopenharmony_ci#define ISL29003_RES_SHIFT (0) 318c2ecf20Sopenharmony_ci#define ISL29003_RES_MASK (0x3 << ISL29003_RES_SHIFT) 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define ISL29003_REG_CONTROL 0x01 348c2ecf20Sopenharmony_ci#define ISL29003_INT_FLG (1 << 5) 358c2ecf20Sopenharmony_ci#define ISL29003_RANGE_SHIFT (2) 368c2ecf20Sopenharmony_ci#define ISL29003_RANGE_MASK (0x3 << ISL29003_RANGE_SHIFT) 378c2ecf20Sopenharmony_ci#define ISL29003_INT_PERSISTS_SHIFT (0) 388c2ecf20Sopenharmony_ci#define ISL29003_INT_PERSISTS_MASK (0xf << ISL29003_INT_PERSISTS_SHIFT) 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci#define ISL29003_REG_IRQ_THRESH_HI 0x02 418c2ecf20Sopenharmony_ci#define ISL29003_REG_IRQ_THRESH_LO 0x03 428c2ecf20Sopenharmony_ci#define ISL29003_REG_LSB_SENSOR 0x04 438c2ecf20Sopenharmony_ci#define ISL29003_REG_MSB_SENSOR 0x05 448c2ecf20Sopenharmony_ci#define ISL29003_REG_LSB_TIMER 0x06 458c2ecf20Sopenharmony_ci#define ISL29003_REG_MSB_TIMER 0x07 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#define ISL29003_NUM_CACHABLE_REGS 4 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistruct isl29003_data { 508c2ecf20Sopenharmony_ci struct i2c_client *client; 518c2ecf20Sopenharmony_ci struct mutex lock; 528c2ecf20Sopenharmony_ci u8 reg_cache[ISL29003_NUM_CACHABLE_REGS]; 538c2ecf20Sopenharmony_ci u8 power_state_before_suspend; 548c2ecf20Sopenharmony_ci}; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic int gain_range[] = { 578c2ecf20Sopenharmony_ci 1000, 4000, 16000, 64000 588c2ecf20Sopenharmony_ci}; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci/* 618c2ecf20Sopenharmony_ci * register access helpers 628c2ecf20Sopenharmony_ci */ 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic int __isl29003_read_reg(struct i2c_client *client, 658c2ecf20Sopenharmony_ci u32 reg, u8 mask, u8 shift) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci struct isl29003_data *data = i2c_get_clientdata(client); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci return (data->reg_cache[reg] & mask) >> shift; 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic int __isl29003_write_reg(struct i2c_client *client, 738c2ecf20Sopenharmony_ci u32 reg, u8 mask, u8 shift, u8 val) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci struct isl29003_data *data = i2c_get_clientdata(client); 768c2ecf20Sopenharmony_ci int ret = 0; 778c2ecf20Sopenharmony_ci u8 tmp; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci if (reg >= ISL29003_NUM_CACHABLE_REGS) 808c2ecf20Sopenharmony_ci return -EINVAL; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci tmp = data->reg_cache[reg]; 858c2ecf20Sopenharmony_ci tmp &= ~mask; 868c2ecf20Sopenharmony_ci tmp |= val << shift; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(client, reg, tmp); 898c2ecf20Sopenharmony_ci if (!ret) 908c2ecf20Sopenharmony_ci data->reg_cache[reg] = tmp; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 938c2ecf20Sopenharmony_ci return ret; 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci/* 978c2ecf20Sopenharmony_ci * internally used functions 988c2ecf20Sopenharmony_ci */ 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/* range */ 1018c2ecf20Sopenharmony_cistatic int isl29003_get_range(struct i2c_client *client) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci return __isl29003_read_reg(client, ISL29003_REG_CONTROL, 1048c2ecf20Sopenharmony_ci ISL29003_RANGE_MASK, ISL29003_RANGE_SHIFT); 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic int isl29003_set_range(struct i2c_client *client, int range) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci return __isl29003_write_reg(client, ISL29003_REG_CONTROL, 1108c2ecf20Sopenharmony_ci ISL29003_RANGE_MASK, ISL29003_RANGE_SHIFT, range); 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci/* resolution */ 1148c2ecf20Sopenharmony_cistatic int isl29003_get_resolution(struct i2c_client *client) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci return __isl29003_read_reg(client, ISL29003_REG_COMMAND, 1178c2ecf20Sopenharmony_ci ISL29003_RES_MASK, ISL29003_RES_SHIFT); 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic int isl29003_set_resolution(struct i2c_client *client, int res) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci return __isl29003_write_reg(client, ISL29003_REG_COMMAND, 1238c2ecf20Sopenharmony_ci ISL29003_RES_MASK, ISL29003_RES_SHIFT, res); 1248c2ecf20Sopenharmony_ci} 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci/* mode */ 1278c2ecf20Sopenharmony_cistatic int isl29003_get_mode(struct i2c_client *client) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci return __isl29003_read_reg(client, ISL29003_REG_COMMAND, 1308c2ecf20Sopenharmony_ci ISL29003_RES_MASK, ISL29003_RES_SHIFT); 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic int isl29003_set_mode(struct i2c_client *client, int mode) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci return __isl29003_write_reg(client, ISL29003_REG_COMMAND, 1368c2ecf20Sopenharmony_ci ISL29003_RES_MASK, ISL29003_RES_SHIFT, mode); 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci/* power_state */ 1408c2ecf20Sopenharmony_cistatic int isl29003_set_power_state(struct i2c_client *client, int state) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci return __isl29003_write_reg(client, ISL29003_REG_COMMAND, 1438c2ecf20Sopenharmony_ci ISL29003_ADC_ENABLED | ISL29003_ADC_PD, 0, 1448c2ecf20Sopenharmony_ci state ? ISL29003_ADC_ENABLED : ISL29003_ADC_PD); 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cistatic int isl29003_get_power_state(struct i2c_client *client) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci struct isl29003_data *data = i2c_get_clientdata(client); 1508c2ecf20Sopenharmony_ci u8 cmdreg = data->reg_cache[ISL29003_REG_COMMAND]; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci return ~cmdreg & ISL29003_ADC_PD; 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cistatic int isl29003_get_adc_value(struct i2c_client *client) 1568c2ecf20Sopenharmony_ci{ 1578c2ecf20Sopenharmony_ci struct isl29003_data *data = i2c_get_clientdata(client); 1588c2ecf20Sopenharmony_ci int lsb, msb, range, bitdepth; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 1618c2ecf20Sopenharmony_ci lsb = i2c_smbus_read_byte_data(client, ISL29003_REG_LSB_SENSOR); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci if (lsb < 0) { 1648c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 1658c2ecf20Sopenharmony_ci return lsb; 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci msb = i2c_smbus_read_byte_data(client, ISL29003_REG_MSB_SENSOR); 1698c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci if (msb < 0) 1728c2ecf20Sopenharmony_ci return msb; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci range = isl29003_get_range(client); 1758c2ecf20Sopenharmony_ci bitdepth = (4 - isl29003_get_resolution(client)) * 4; 1768c2ecf20Sopenharmony_ci return (((msb << 8) | lsb) * gain_range[range]) >> bitdepth; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci/* 1808c2ecf20Sopenharmony_ci * sysfs layer 1818c2ecf20Sopenharmony_ci */ 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci/* range */ 1848c2ecf20Sopenharmony_cistatic ssize_t isl29003_show_range(struct device *dev, 1858c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 1868c2ecf20Sopenharmony_ci{ 1878c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci return sprintf(buf, "%i\n", isl29003_get_range(client)); 1908c2ecf20Sopenharmony_ci} 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_cistatic ssize_t isl29003_store_range(struct device *dev, 1938c2ecf20Sopenharmony_ci struct device_attribute *attr, 1948c2ecf20Sopenharmony_ci const char *buf, size_t count) 1958c2ecf20Sopenharmony_ci{ 1968c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 1978c2ecf20Sopenharmony_ci unsigned long val; 1988c2ecf20Sopenharmony_ci int ret; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci ret = kstrtoul(buf, 10, &val); 2018c2ecf20Sopenharmony_ci if (ret) 2028c2ecf20Sopenharmony_ci return ret; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci if (val > 3) 2058c2ecf20Sopenharmony_ci return -EINVAL; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci ret = isl29003_set_range(client, val); 2088c2ecf20Sopenharmony_ci if (ret < 0) 2098c2ecf20Sopenharmony_ci return ret; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci return count; 2128c2ecf20Sopenharmony_ci} 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_cistatic DEVICE_ATTR(range, S_IWUSR | S_IRUGO, 2158c2ecf20Sopenharmony_ci isl29003_show_range, isl29003_store_range); 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci/* resolution */ 2198c2ecf20Sopenharmony_cistatic ssize_t isl29003_show_resolution(struct device *dev, 2208c2ecf20Sopenharmony_ci struct device_attribute *attr, 2218c2ecf20Sopenharmony_ci char *buf) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", isl29003_get_resolution(client)); 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic ssize_t isl29003_store_resolution(struct device *dev, 2298c2ecf20Sopenharmony_ci struct device_attribute *attr, 2308c2ecf20Sopenharmony_ci const char *buf, size_t count) 2318c2ecf20Sopenharmony_ci{ 2328c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 2338c2ecf20Sopenharmony_ci unsigned long val; 2348c2ecf20Sopenharmony_ci int ret; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci ret = kstrtoul(buf, 10, &val); 2378c2ecf20Sopenharmony_ci if (ret) 2388c2ecf20Sopenharmony_ci return ret; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci if (val > 3) 2418c2ecf20Sopenharmony_ci return -EINVAL; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci ret = isl29003_set_resolution(client, val); 2448c2ecf20Sopenharmony_ci if (ret < 0) 2458c2ecf20Sopenharmony_ci return ret; 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci return count; 2488c2ecf20Sopenharmony_ci} 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_cistatic DEVICE_ATTR(resolution, S_IWUSR | S_IRUGO, 2518c2ecf20Sopenharmony_ci isl29003_show_resolution, isl29003_store_resolution); 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci/* mode */ 2548c2ecf20Sopenharmony_cistatic ssize_t isl29003_show_mode(struct device *dev, 2558c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2568c2ecf20Sopenharmony_ci{ 2578c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", isl29003_get_mode(client)); 2608c2ecf20Sopenharmony_ci} 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_cistatic ssize_t isl29003_store_mode(struct device *dev, 2638c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t count) 2648c2ecf20Sopenharmony_ci{ 2658c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 2668c2ecf20Sopenharmony_ci unsigned long val; 2678c2ecf20Sopenharmony_ci int ret; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci ret = kstrtoul(buf, 10, &val); 2708c2ecf20Sopenharmony_ci if (ret) 2718c2ecf20Sopenharmony_ci return ret; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci if (val > 2) 2748c2ecf20Sopenharmony_ci return -EINVAL; 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci ret = isl29003_set_mode(client, val); 2778c2ecf20Sopenharmony_ci if (ret < 0) 2788c2ecf20Sopenharmony_ci return ret; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci return count; 2818c2ecf20Sopenharmony_ci} 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_cistatic DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, 2848c2ecf20Sopenharmony_ci isl29003_show_mode, isl29003_store_mode); 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci/* power state */ 2888c2ecf20Sopenharmony_cistatic ssize_t isl29003_show_power_state(struct device *dev, 2898c2ecf20Sopenharmony_ci struct device_attribute *attr, 2908c2ecf20Sopenharmony_ci char *buf) 2918c2ecf20Sopenharmony_ci{ 2928c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", isl29003_get_power_state(client)); 2958c2ecf20Sopenharmony_ci} 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_cistatic ssize_t isl29003_store_power_state(struct device *dev, 2988c2ecf20Sopenharmony_ci struct device_attribute *attr, 2998c2ecf20Sopenharmony_ci const char *buf, size_t count) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 3028c2ecf20Sopenharmony_ci unsigned long val; 3038c2ecf20Sopenharmony_ci int ret; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci ret = kstrtoul(buf, 10, &val); 3068c2ecf20Sopenharmony_ci if (ret) 3078c2ecf20Sopenharmony_ci return ret; 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci if (val > 1) 3108c2ecf20Sopenharmony_ci return -EINVAL; 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci ret = isl29003_set_power_state(client, val); 3138c2ecf20Sopenharmony_ci return ret ? ret : count; 3148c2ecf20Sopenharmony_ci} 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_cistatic DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO, 3178c2ecf20Sopenharmony_ci isl29003_show_power_state, isl29003_store_power_state); 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci/* lux */ 3218c2ecf20Sopenharmony_cistatic ssize_t isl29003_show_lux(struct device *dev, 3228c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 3238c2ecf20Sopenharmony_ci{ 3248c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci /* No LUX data if not operational */ 3278c2ecf20Sopenharmony_ci if (!isl29003_get_power_state(client)) 3288c2ecf20Sopenharmony_ci return -EBUSY; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", isl29003_get_adc_value(client)); 3318c2ecf20Sopenharmony_ci} 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_cistatic DEVICE_ATTR(lux, S_IRUGO, isl29003_show_lux, NULL); 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_cistatic struct attribute *isl29003_attributes[] = { 3368c2ecf20Sopenharmony_ci &dev_attr_range.attr, 3378c2ecf20Sopenharmony_ci &dev_attr_resolution.attr, 3388c2ecf20Sopenharmony_ci &dev_attr_mode.attr, 3398c2ecf20Sopenharmony_ci &dev_attr_power_state.attr, 3408c2ecf20Sopenharmony_ci &dev_attr_lux.attr, 3418c2ecf20Sopenharmony_ci NULL 3428c2ecf20Sopenharmony_ci}; 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_cistatic const struct attribute_group isl29003_attr_group = { 3458c2ecf20Sopenharmony_ci .attrs = isl29003_attributes, 3468c2ecf20Sopenharmony_ci}; 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_cistatic int isl29003_init_client(struct i2c_client *client) 3498c2ecf20Sopenharmony_ci{ 3508c2ecf20Sopenharmony_ci struct isl29003_data *data = i2c_get_clientdata(client); 3518c2ecf20Sopenharmony_ci int i; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci /* read all the registers once to fill the cache. 3548c2ecf20Sopenharmony_ci * if one of the reads fails, we consider the init failed */ 3558c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(data->reg_cache); i++) { 3568c2ecf20Sopenharmony_ci int v = i2c_smbus_read_byte_data(client, i); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci if (v < 0) 3598c2ecf20Sopenharmony_ci return -ENODEV; 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci data->reg_cache[i] = v; 3628c2ecf20Sopenharmony_ci } 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci /* set defaults */ 3658c2ecf20Sopenharmony_ci isl29003_set_range(client, 0); 3668c2ecf20Sopenharmony_ci isl29003_set_resolution(client, 0); 3678c2ecf20Sopenharmony_ci isl29003_set_mode(client, 0); 3688c2ecf20Sopenharmony_ci isl29003_set_power_state(client, 0); 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci return 0; 3718c2ecf20Sopenharmony_ci} 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci/* 3748c2ecf20Sopenharmony_ci * I2C layer 3758c2ecf20Sopenharmony_ci */ 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_cistatic int isl29003_probe(struct i2c_client *client, 3788c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 3798c2ecf20Sopenharmony_ci{ 3808c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = client->adapter; 3818c2ecf20Sopenharmony_ci struct isl29003_data *data; 3828c2ecf20Sopenharmony_ci int err = 0; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) 3858c2ecf20Sopenharmony_ci return -EIO; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci data = kzalloc(sizeof(struct isl29003_data), GFP_KERNEL); 3888c2ecf20Sopenharmony_ci if (!data) 3898c2ecf20Sopenharmony_ci return -ENOMEM; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci data->client = client; 3928c2ecf20Sopenharmony_ci i2c_set_clientdata(client, data); 3938c2ecf20Sopenharmony_ci mutex_init(&data->lock); 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci /* initialize the ISL29003 chip */ 3968c2ecf20Sopenharmony_ci err = isl29003_init_client(client); 3978c2ecf20Sopenharmony_ci if (err) 3988c2ecf20Sopenharmony_ci goto exit_kfree; 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci /* register sysfs hooks */ 4018c2ecf20Sopenharmony_ci err = sysfs_create_group(&client->dev.kobj, &isl29003_attr_group); 4028c2ecf20Sopenharmony_ci if (err) 4038c2ecf20Sopenharmony_ci goto exit_kfree; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci dev_info(&client->dev, "driver version %s enabled\n", DRIVER_VERSION); 4068c2ecf20Sopenharmony_ci return 0; 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ciexit_kfree: 4098c2ecf20Sopenharmony_ci kfree(data); 4108c2ecf20Sopenharmony_ci return err; 4118c2ecf20Sopenharmony_ci} 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_cistatic int isl29003_remove(struct i2c_client *client) 4148c2ecf20Sopenharmony_ci{ 4158c2ecf20Sopenharmony_ci sysfs_remove_group(&client->dev.kobj, &isl29003_attr_group); 4168c2ecf20Sopenharmony_ci isl29003_set_power_state(client, 0); 4178c2ecf20Sopenharmony_ci kfree(i2c_get_clientdata(client)); 4188c2ecf20Sopenharmony_ci return 0; 4198c2ecf20Sopenharmony_ci} 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 4228c2ecf20Sopenharmony_cistatic int isl29003_suspend(struct device *dev) 4238c2ecf20Sopenharmony_ci{ 4248c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 4258c2ecf20Sopenharmony_ci struct isl29003_data *data = i2c_get_clientdata(client); 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci data->power_state_before_suspend = isl29003_get_power_state(client); 4288c2ecf20Sopenharmony_ci return isl29003_set_power_state(client, 0); 4298c2ecf20Sopenharmony_ci} 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_cistatic int isl29003_resume(struct device *dev) 4328c2ecf20Sopenharmony_ci{ 4338c2ecf20Sopenharmony_ci int i; 4348c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 4358c2ecf20Sopenharmony_ci struct isl29003_data *data = i2c_get_clientdata(client); 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci /* restore registers from cache */ 4388c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(data->reg_cache); i++) 4398c2ecf20Sopenharmony_ci if (i2c_smbus_write_byte_data(client, i, data->reg_cache[i])) 4408c2ecf20Sopenharmony_ci return -EIO; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci return isl29003_set_power_state(client, 4438c2ecf20Sopenharmony_ci data->power_state_before_suspend); 4448c2ecf20Sopenharmony_ci} 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(isl29003_pm_ops, isl29003_suspend, isl29003_resume); 4478c2ecf20Sopenharmony_ci#define ISL29003_PM_OPS (&isl29003_pm_ops) 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci#else 4508c2ecf20Sopenharmony_ci#define ISL29003_PM_OPS NULL 4518c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */ 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_cistatic const struct i2c_device_id isl29003_id[] = { 4548c2ecf20Sopenharmony_ci { "isl29003", 0 }, 4558c2ecf20Sopenharmony_ci {} 4568c2ecf20Sopenharmony_ci}; 4578c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, isl29003_id); 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_cistatic struct i2c_driver isl29003_driver = { 4608c2ecf20Sopenharmony_ci .driver = { 4618c2ecf20Sopenharmony_ci .name = ISL29003_DRV_NAME, 4628c2ecf20Sopenharmony_ci .pm = ISL29003_PM_OPS, 4638c2ecf20Sopenharmony_ci }, 4648c2ecf20Sopenharmony_ci .probe = isl29003_probe, 4658c2ecf20Sopenharmony_ci .remove = isl29003_remove, 4668c2ecf20Sopenharmony_ci .id_table = isl29003_id, 4678c2ecf20Sopenharmony_ci}; 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_cimodule_i2c_driver(isl29003_driver); 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ciMODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); 4728c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ISL29003 ambient light sensor driver"); 4738c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 4748c2ecf20Sopenharmony_ciMODULE_VERSION(DRIVER_VERSION); 475