Skip to content

Commit d5d8f49

Browse files
Crestez Dan Leonardjic23
authored andcommitted
max44000: Expose ambient sensor scaling
This patch exposes ALSTIM as illuminance_integration_time and ALSPGA as illuminance_scale. Changing ALSTIM also changes the number of bits available in the data register. This is handled inside raw value reading because: * It's very easy to shift a few bits * It allows SCALE and INT_TIME to be completely independent controls * Buffer support requires constant scan_type.realbits per-channel Signed-off-by: Crestez Dan Leonard <leonard.crestez@intel.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 237a378 commit d5d8f49

1 file changed

Lines changed: 162 additions & 5 deletions

File tree

‎drivers/iio/light/max44000.c‎

Lines changed: 162 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
*/
6060
#define MAX44000_REG_CFG_RX_DEFAULT 0xf0
6161

62+
/* REG_RX bits */
63+
#define MAX44000_CFG_RX_ALSTIM_MASK 0x0c
64+
#define MAX44000_CFG_RX_ALSTIM_SHIFT 2
65+
#define MAX44000_CFG_RX_ALSPGA_MASK 0x03
66+
#define MAX44000_CFG_RX_ALSPGA_SHIFT 0
67+
6268
/* REG_TX bits */
6369
#define MAX44000_LED_CURRENT_MASK 0xf
6470
#define MAX44000_LED_CURRENT_MAX 11
@@ -74,11 +80,57 @@ struct max44000_data {
7480
/* Default scale is set to the minimum of 0.03125 or 1 / (1 << 5) lux */
7581
#define MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2 5
7682

83+
/* Scale can be multiplied by up to 128x via ALSPGA for measurement gain */
84+
static const int max44000_alspga_shift[] = {0, 2, 4, 7};
85+
#define MAX44000_ALSPGA_MAX_SHIFT 7
86+
87+
/*
88+
* Scale can be multiplied by up to 64x via ALSTIM because of lost resolution
89+
*
90+
* This scaling factor is hidden from userspace and instead accounted for when
91+
* reading raw values from the device.
92+
*
93+
* This makes it possible to cleanly expose ALSPGA as IIO_CHAN_INFO_SCALE and
94+
* ALSTIM as IIO_CHAN_INFO_INT_TIME without the values affecting each other.
95+
*
96+
* Handling this internally is also required for buffer support because the
97+
* channel's scan_type can't be modified dynamically.
98+
*/
99+
static const int max44000_alstim_shift[] = {0, 2, 4, 6};
100+
#define MAX44000_ALSTIM_SHIFT(alstim) (2 * (alstim))
101+
102+
/* Available integration times with pretty manual alignment: */
103+
static const int max44000_int_time_avail_ns_array[] = {
104+
100000000,
105+
25000000,
106+
6250000,
107+
1562500,
108+
};
109+
static const char max44000_int_time_avail_str[] =
110+
"0.100 "
111+
"0.025 "
112+
"0.00625 "
113+
"0.001625";
114+
115+
/* Available scales (internal to ulux) with pretty manual alignment: */
116+
static const int max44000_scale_avail_ulux_array[] = {
117+
31250,
118+
125000,
119+
500000,
120+
4000000,
121+
};
122+
static const char max44000_scale_avail_str[] =
123+
"0.03125 "
124+
"0.125 "
125+
"0.5 "
126+
"4";
127+
77128
static const struct iio_chan_spec max44000_channels[] = {
78129
{
79130
.type = IIO_LIGHT,
80131
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
81-
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
132+
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
133+
BIT(IIO_CHAN_INFO_INT_TIME),
82134
},
83135
{
84136
.type = IIO_PROXIMITY,
@@ -94,15 +146,54 @@ static const struct iio_chan_spec max44000_channels[] = {
94146
},
95147
};
96148

149+
static int max44000_read_alstim(struct max44000_data *data)
150+
{
151+
unsigned int val;
152+
int ret;
153+
154+
ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val);
155+
if (ret < 0)
156+
return ret;
157+
return (val & MAX44000_CFG_RX_ALSTIM_MASK) >> MAX44000_CFG_RX_ALSTIM_SHIFT;
158+
}
159+
160+
static int max44000_write_alstim(struct max44000_data *data, int val)
161+
{
162+
return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX,
163+
MAX44000_CFG_RX_ALSTIM_MASK,
164+
val << MAX44000_CFG_RX_ALSTIM_SHIFT);
165+
}
166+
167+
static int max44000_read_alspga(struct max44000_data *data)
168+
{
169+
unsigned int val;
170+
int ret;
171+
172+
ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val);
173+
if (ret < 0)
174+
return ret;
175+
return (val & MAX44000_CFG_RX_ALSPGA_MASK) >> MAX44000_CFG_RX_ALSPGA_SHIFT;
176+
}
177+
178+
static int max44000_write_alspga(struct max44000_data *data, int val)
179+
{
180+
return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX,
181+
MAX44000_CFG_RX_ALSPGA_MASK,
182+
val << MAX44000_CFG_RX_ALSPGA_SHIFT);
183+
}
184+
97185
static int max44000_read_alsval(struct max44000_data *data)
98186
{
99187
u16 regval;
100-
int ret;
188+
int alstim, ret;
101189

102190
ret = regmap_bulk_read(data->regmap, MAX44000_REG_ALS_DATA_HI,
103191
&regval, sizeof(regval));
104192
if (ret < 0)
105193
return ret;
194+
alstim = ret = max44000_read_alstim(data);
195+
if (ret < 0)
196+
return ret;
106197

107198
regval = be16_to_cpu(regval);
108199

@@ -118,7 +209,7 @@ static int max44000_read_alsval(struct max44000_data *data)
118209
if (regval & MAX44000_ALSDATA_OVERFLOW)
119210
return 0x3FFF;
120211

121-
return regval;
212+
return regval << MAX44000_ALSTIM_SHIFT(alstim);
122213
}
123214

124215
static int max44000_write_led_current_raw(struct max44000_data *data, int val)
@@ -151,6 +242,7 @@ static int max44000_read_raw(struct iio_dev *indio_dev,
151242
int *val, int *val2, long mask)
152243
{
153244
struct max44000_data *data = iio_priv(indio_dev);
245+
int alstim, alspga;
154246
unsigned int regval;
155247
int ret;
156248

@@ -196,14 +288,34 @@ static int max44000_read_raw(struct iio_dev *indio_dev,
196288
return IIO_VAL_INT;
197289

198290
case IIO_LIGHT:
199-
*val = 1;
200-
*val2 = MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2;
291+
mutex_lock(&data->lock);
292+
alspga = ret = max44000_read_alspga(data);
293+
mutex_unlock(&data->lock);
294+
if (ret < 0)
295+
return ret;
296+
297+
/* Avoid negative shifts */
298+
*val = (1 << MAX44000_ALSPGA_MAX_SHIFT);
299+
*val2 = MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2
300+
+ MAX44000_ALSPGA_MAX_SHIFT
301+
- max44000_alspga_shift[alspga];
201302
return IIO_VAL_FRACTIONAL_LOG2;
202303

203304
default:
204305
return -EINVAL;
205306
}
206307

308+
case IIO_CHAN_INFO_INT_TIME:
309+
mutex_lock(&data->lock);
310+
alstim = ret = max44000_read_alstim(data);
311+
mutex_unlock(&data->lock);
312+
313+
if (ret < 0)
314+
return ret;
315+
*val = 0;
316+
*val2 = max44000_int_time_avail_ns_array[alstim];
317+
return IIO_VAL_INT_PLUS_NANO;
318+
207319
default:
208320
return -EINVAL;
209321
}
@@ -221,15 +333,60 @@ static int max44000_write_raw(struct iio_dev *indio_dev,
221333
ret = max44000_write_led_current_raw(data, val);
222334
mutex_unlock(&data->lock);
223335
return ret;
336+
} else if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) {
337+
s64 valns = val * NSEC_PER_SEC + val2;
338+
int alstim = find_closest_descending(valns,
339+
max44000_int_time_avail_ns_array,
340+
ARRAY_SIZE(max44000_int_time_avail_ns_array));
341+
mutex_lock(&data->lock);
342+
ret = max44000_write_alstim(data, alstim);
343+
mutex_unlock(&data->lock);
344+
return ret;
345+
} else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT) {
346+
s64 valus = val * USEC_PER_SEC + val2;
347+
int alspga = find_closest(valus,
348+
max44000_scale_avail_ulux_array,
349+
ARRAY_SIZE(max44000_scale_avail_ulux_array));
350+
mutex_lock(&data->lock);
351+
ret = max44000_write_alspga(data, alspga);
352+
mutex_unlock(&data->lock);
353+
return ret;
224354
}
225355

226356
return -EINVAL;
227357
}
228358

359+
static int max44000_write_raw_get_fmt(struct iio_dev *indio_dev,
360+
struct iio_chan_spec const *chan,
361+
long mask)
362+
{
363+
if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT)
364+
return IIO_VAL_INT_PLUS_NANO;
365+
else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT)
366+
return IIO_VAL_INT_PLUS_MICRO;
367+
else
368+
return IIO_VAL_INT;
369+
}
370+
371+
static IIO_CONST_ATTR(illuminance_integration_time_available, max44000_int_time_avail_str);
372+
static IIO_CONST_ATTR(illuminance_scale_available, max44000_scale_avail_str);
373+
374+
static struct attribute *max44000_attributes[] = {
375+
&iio_const_attr_illuminance_integration_time_available.dev_attr.attr,
376+
&iio_const_attr_illuminance_scale_available.dev_attr.attr,
377+
NULL
378+
};
379+
380+
static const struct attribute_group max44000_attribute_group = {
381+
.attrs = max44000_attributes,
382+
};
383+
229384
static const struct iio_info max44000_info = {
230385
.driver_module = THIS_MODULE,
231386
.read_raw = max44000_read_raw,
232387
.write_raw = max44000_write_raw,
388+
.write_raw_get_fmt = max44000_write_raw_get_fmt,
389+
.attrs = &max44000_attribute_group,
233390
};
234391

235392
static bool max44000_readable_reg(struct device *dev, unsigned int reg)

0 commit comments

Comments
 (0)