^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * apds9802als.c - apds9802 ALS Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2009 Intel Corp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define ALS_MIN_RANGE_VAL 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define ALS_MAX_RANGE_VAL 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define POWER_STA_ENABLE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define POWER_STA_DISABLE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define DRIVER_NAME "apds9802als"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct als_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static ssize_t als_sensing_range_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) val = i2c_smbus_read_byte_data(client, 0x81);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (val & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return sprintf(buf, "4095\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return sprintf(buf, "65535\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int als_wait_for_data_ready(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int retry = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) msleep(30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ret = i2c_smbus_read_byte_data(client, 0x86);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) } while (!(ret & 0x80) && retry--);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (retry < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) dev_warn(dev, "timeout waiting for data ready\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static ssize_t als_lux0_input_data_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct als_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* Protect against parallel reads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) pm_runtime_get_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) mutex_lock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* clear EOC interrupt status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) i2c_smbus_write_byte(client, 0x40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /* start measurement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) temp = i2c_smbus_read_byte_data(client, 0x81);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) i2c_smbus_write_byte_data(client, 0x81, temp | 0x08);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) ret_val = als_wait_for_data_ready(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (ret_val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) temp = i2c_smbus_read_byte_data(client, 0x8C); /* LSB data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (temp < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ret_val = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ret_val = i2c_smbus_read_byte_data(client, 0x8D); /* MSB data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (ret_val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) pm_runtime_put_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) temp = (ret_val << 8) | temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return sprintf(buf, "%d\n", temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) pm_runtime_put_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static ssize_t als_sensing_range_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct als_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ret_val = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (ret_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (val < 4096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) val = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) else if (val < 65536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) val = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pm_runtime_get_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* Make sure nobody else reads/modifies/writes 0x81 while we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) are active */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) mutex_lock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret_val = i2c_smbus_read_byte_data(client, 0x81);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (ret_val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* Reset the bits before setting them */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ret_val = ret_val & 0xFA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (val == 1) /* Setting detection range up to 4k LUX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ret_val = (ret_val | 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) else /* Setting detection range up to 64k LUX*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ret_val = (ret_val | 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) ret_val = i2c_smbus_write_byte_data(client, 0x81, ret_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (ret_val >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* All OK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) pm_runtime_put_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) pm_runtime_put_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int als_set_power_state(struct i2c_client *client, bool on_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct als_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) mutex_lock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ret_val = i2c_smbus_read_byte_data(client, 0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (ret_val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (on_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ret_val = ret_val | 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ret_val = ret_val & 0xFE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ret_val = i2c_smbus_write_byte_data(client, 0x80, ret_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static DEVICE_ATTR(lux0_sensor_range, S_IRUGO | S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) als_sensing_range_show, als_sensing_range_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static DEVICE_ATTR(lux0_input, S_IRUGO, als_lux0_input_data_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static struct attribute *mid_att_als[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) &dev_attr_lux0_sensor_range.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) &dev_attr_lux0_input.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static const struct attribute_group m_als_gr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .name = "apds9802als",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .attrs = mid_att_als
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int als_set_default_config(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /* Write the command and then switch on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ret_val = i2c_smbus_write_byte_data(client, 0x80, 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (ret_val < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) dev_err(&client->dev, "failed default switch on write\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /* detection range: 1~64K Lux, maunal measurement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) ret_val = i2c_smbus_write_byte_data(client, 0x81, 0x08);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (ret_val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) dev_err(&client->dev, "failed default LUX on write\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /* We always get 0 for the 1st measurement after system power on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * so make sure it is finished before user asks for data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) als_wait_for_data_ready(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static int apds9802als_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct als_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) data = kzalloc(sizeof(struct als_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (data == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) dev_err(&client->dev, "Memory allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) res = sysfs_create_group(&client->dev.kobj, &m_als_gr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) dev_err(&client->dev, "device create file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) goto als_error1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) dev_info(&client->dev, "ALS chip found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) als_set_default_config(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) mutex_init(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) pm_runtime_set_active(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) pm_runtime_enable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) als_error1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static int apds9802als_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) struct als_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) pm_runtime_get_sync(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) als_set_power_state(client, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) sysfs_remove_group(&client->dev.kobj, &m_als_gr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) pm_runtime_disable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) pm_runtime_set_suspended(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) pm_runtime_put_noidle(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int apds9802als_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) als_set_power_state(client, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static int apds9802als_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) als_set_power_state(client, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static UNIVERSAL_DEV_PM_OPS(apds9802als_pm_ops, apds9802als_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) apds9802als_resume, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) #define APDS9802ALS_PM_OPS (&apds9802als_pm_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) #else /* CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) #define APDS9802ALS_PM_OPS NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) #endif /* CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static const struct i2c_device_id apds9802als_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) { DRIVER_NAME, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) MODULE_DEVICE_TABLE(i2c, apds9802als_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static struct i2c_driver apds9802als_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) .name = DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) .pm = APDS9802ALS_PM_OPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) .probe = apds9802als_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) .remove = apds9802als_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) .id_table = apds9802als_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) module_i2c_driver(apds9802als_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) MODULE_AUTHOR("Anantha Narayanan <Anantha.Narayanan@intel.com");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) MODULE_DESCRIPTION("Avago apds9802als ALS Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) MODULE_LICENSE("GPL v2");