2019-05-27 14:55:21 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2012-03-04 04:19:52 +08:00
|
|
|
/*
|
2020-02-12 02:05:57 +08:00
|
|
|
* drivers/media/i2c/ccs/ccs-reg-access.c
|
2012-03-04 04:19:52 +08:00
|
|
|
*
|
2020-02-12 02:05:57 +08:00
|
|
|
* Generic driver for MIPI CCS/SMIA/SMIA++ compliant camera sensors
|
2012-03-04 04:19:52 +08:00
|
|
|
*
|
2020-02-12 02:05:57 +08:00
|
|
|
* Copyright (C) 2020 Intel Corporation
|
2012-03-04 04:19:52 +08:00
|
|
|
* Copyright (C) 2011--2012 Nokia Corporation
|
2020-06-25 05:57:46 +08:00
|
|
|
* Contact: Sakari Ailus <sakari.ailus@linux.intel.com>
|
2012-03-04 04:19:52 +08:00
|
|
|
*/
|
|
|
|
|
2020-02-03 19:36:55 +08:00
|
|
|
#include <asm/unaligned.h>
|
|
|
|
|
2012-03-04 04:19:52 +08:00
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
|
2020-02-11 21:19:13 +08:00
|
|
|
#include "ccs.h"
|
2020-06-24 21:39:05 +08:00
|
|
|
#include "ccs-limits.h"
|
2012-03-04 04:19:52 +08:00
|
|
|
|
|
|
|
static uint32_t float_to_u32_mul_1000000(struct i2c_client *client,
|
|
|
|
uint32_t phloat)
|
|
|
|
{
|
|
|
|
int32_t exp;
|
|
|
|
uint64_t man;
|
|
|
|
|
|
|
|
if (phloat >= 0x80000000) {
|
|
|
|
dev_err(&client->dev, "this is a negative number\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (phloat == 0x7f800000)
|
|
|
|
return ~0; /* Inf. */
|
|
|
|
|
|
|
|
if ((phloat & 0x7f800000) == 0x7f800000) {
|
|
|
|
dev_err(&client->dev, "NaN or other special number\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Valid cases begin here */
|
|
|
|
if (phloat == 0)
|
|
|
|
return 0; /* Valid zero */
|
|
|
|
|
|
|
|
if (phloat > 0x4f800000)
|
|
|
|
return ~0; /* larger than 4294967295 */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unbias exponent (note how phloat is now guaranteed to
|
|
|
|
* have 0 in the high bit)
|
|
|
|
*/
|
|
|
|
exp = ((int32_t)phloat >> 23) - 127;
|
|
|
|
|
|
|
|
/* Extract mantissa, add missing '1' bit and it's in MHz */
|
|
|
|
man = ((phloat & 0x7fffff) | 0x800000) * 1000000ULL;
|
|
|
|
|
|
|
|
if (exp < 0)
|
|
|
|
man >>= -exp;
|
|
|
|
else
|
|
|
|
man <<= exp;
|
|
|
|
|
|
|
|
man >>= 23; /* Remove mantissa bias */
|
|
|
|
|
|
|
|
return man & 0xffffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a 8/16/32-bit i2c register. The value is returned in 'val'.
|
|
|
|
* Returns zero if successful, or non-zero otherwise.
|
|
|
|
*/
|
2020-02-11 21:19:13 +08:00
|
|
|
static int ____ccs_read_addr(struct ccs_sensor *sensor, u16 reg, u16 len,
|
2020-02-11 19:16:14 +08:00
|
|
|
u32 *val)
|
2012-03-04 04:19:52 +08:00
|
|
|
{
|
2012-04-22 19:55:10 +08:00
|
|
|
struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
|
2012-03-04 04:19:52 +08:00
|
|
|
struct i2c_msg msg;
|
2020-02-03 19:36:55 +08:00
|
|
|
unsigned char data_buf[sizeof(u32)] = { 0 };
|
|
|
|
unsigned char offset_buf[sizeof(u16)];
|
2012-03-04 04:19:52 +08:00
|
|
|
int r;
|
|
|
|
|
2020-02-03 19:36:55 +08:00
|
|
|
if (len > sizeof(data_buf))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-03-04 04:19:52 +08:00
|
|
|
msg.addr = client->addr;
|
|
|
|
msg.flags = 0;
|
2020-02-03 19:36:55 +08:00
|
|
|
msg.len = sizeof(offset_buf);
|
|
|
|
msg.buf = offset_buf;
|
|
|
|
put_unaligned_be16(reg, offset_buf);
|
2012-03-04 04:19:52 +08:00
|
|
|
|
|
|
|
r = i2c_transfer(client->adapter, &msg, 1);
|
|
|
|
if (r != 1) {
|
|
|
|
if (r >= 0)
|
|
|
|
r = -EBUSY;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.len = len;
|
|
|
|
msg.flags = I2C_M_RD;
|
2020-02-03 19:36:55 +08:00
|
|
|
msg.buf = &data_buf[sizeof(data_buf) - len];
|
|
|
|
|
2012-03-04 04:19:52 +08:00
|
|
|
r = i2c_transfer(client->adapter, &msg, 1);
|
|
|
|
if (r != 1) {
|
|
|
|
if (r >= 0)
|
|
|
|
r = -EBUSY;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2020-02-03 19:36:55 +08:00
|
|
|
*val = get_unaligned_be32(data_buf);
|
2012-03-04 04:19:52 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2020-02-03 19:36:55 +08:00
|
|
|
dev_err(&client->dev, "read from offset 0x%x error %d\n", reg, r);
|
2012-03-04 04:19:52 +08:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-04-22 20:11:26 +08:00
|
|
|
/* Read a register using 8-bit access only. */
|
2020-02-11 21:19:13 +08:00
|
|
|
static int ____ccs_read_addr_8only(struct ccs_sensor *sensor, u16 reg,
|
2020-02-11 19:16:14 +08:00
|
|
|
u16 len, u32 *val)
|
2012-04-22 20:11:26 +08:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
*val = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
u32 val8;
|
|
|
|
|
2020-02-11 19:16:14 +08:00
|
|
|
rval = ____ccs_read_addr(sensor, reg + i, 1, &val8);
|
2012-04-22 20:11:26 +08:00
|
|
|
if (rval < 0)
|
|
|
|
return rval;
|
|
|
|
*val |= val8 << ((len - i - 1) << 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-03 19:38:42 +08:00
|
|
|
unsigned int ccs_reg_width(u32 reg)
|
|
|
|
{
|
|
|
|
if (reg & CCS_FL_16BIT)
|
|
|
|
return sizeof(uint16_t);
|
|
|
|
if (reg & CCS_FL_32BIT)
|
|
|
|
return sizeof(uint32_t);
|
|
|
|
|
|
|
|
return sizeof(uint8_t);
|
|
|
|
}
|
|
|
|
|
2020-06-24 21:39:05 +08:00
|
|
|
static u32 ireal32_to_u32_mul_1000000(struct i2c_client *client, u32 val)
|
|
|
|
{
|
|
|
|
if (val >> 10 > U32_MAX / 15625) {
|
|
|
|
dev_warn(&client->dev, "value %u overflows!\n", val);
|
|
|
|
return U32_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((val >> 10) * 15625) +
|
|
|
|
(val & GENMASK(9, 0)) * 15625 / 1024;
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:23:40 +08:00
|
|
|
u32 ccs_reg_conv(struct ccs_sensor *sensor, u32 reg, u32 val)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
|
|
|
|
|
2020-06-24 21:39:05 +08:00
|
|
|
if (reg & CCS_FL_FLOAT_IREAL) {
|
|
|
|
if (CCS_LIM(sensor, CLOCK_CAPA_TYPE_CAPABILITY) &
|
|
|
|
CCS_CLOCK_CAPA_TYPE_CAPABILITY_IREAL)
|
|
|
|
val = ireal32_to_u32_mul_1000000(client, val);
|
|
|
|
else
|
|
|
|
val = float_to_u32_mul_1000000(client, val);
|
|
|
|
} else if (reg & CCS_FL_IREAL) {
|
|
|
|
val = ireal32_to_u32_mul_1000000(client, val);
|
|
|
|
}
|
2020-09-02 18:23:40 +08:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2012-04-22 20:11:26 +08:00
|
|
|
/*
|
|
|
|
* Read a 8/16/32-bit i2c register. The value is returned in 'val'.
|
|
|
|
* Returns zero if successful, or non-zero otherwise.
|
|
|
|
*/
|
2020-02-11 21:19:13 +08:00
|
|
|
static int __ccs_read_addr(struct ccs_sensor *sensor, u32 reg, u32 *val,
|
2020-09-02 18:23:40 +08:00
|
|
|
bool only8, bool conv)
|
2012-04-22 20:11:26 +08:00
|
|
|
{
|
2020-02-03 19:38:42 +08:00
|
|
|
unsigned int len = ccs_reg_width(reg);
|
2012-04-22 20:11:26 +08:00
|
|
|
int rval;
|
|
|
|
|
2020-02-03 19:02:45 +08:00
|
|
|
if (!only8)
|
2020-02-11 21:19:13 +08:00
|
|
|
rval = ____ccs_read_addr(sensor, CCS_REG_ADDR(reg), len, val);
|
2012-04-22 20:11:26 +08:00
|
|
|
else
|
2020-02-11 21:19:13 +08:00
|
|
|
rval = ____ccs_read_addr_8only(sensor, CCS_REG_ADDR(reg), len,
|
|
|
|
val);
|
2012-04-22 20:11:26 +08:00
|
|
|
if (rval < 0)
|
|
|
|
return rval;
|
|
|
|
|
2020-09-02 18:23:40 +08:00
|
|
|
if (!conv)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*val = ccs_reg_conv(sensor, reg, *val);
|
2012-04-22 20:11:26 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:09:03 +08:00
|
|
|
static int ccs_read_addr_raw(struct ccs_sensor *sensor, u32 reg, u32 *val,
|
2020-09-02 18:23:40 +08:00
|
|
|
bool force8, bool quirk, bool conv)
|
2014-04-01 19:37:38 +08:00
|
|
|
{
|
|
|
|
int rval;
|
|
|
|
|
2020-09-02 18:09:03 +08:00
|
|
|
if (quirk) {
|
|
|
|
*val = 0;
|
|
|
|
rval = ccs_call_quirk(sensor, reg_access, false, ®, val);
|
|
|
|
if (rval == -ENOIOCTLCMD)
|
|
|
|
return 0;
|
|
|
|
if (rval < 0)
|
|
|
|
return rval;
|
2014-04-01 19:37:38 +08:00
|
|
|
|
2020-09-02 18:09:03 +08:00
|
|
|
if (force8)
|
2020-09-02 18:23:40 +08:00
|
|
|
return __ccs_read_addr(sensor, reg, val, true, conv);
|
2020-09-02 18:09:03 +08:00
|
|
|
}
|
2014-12-14 04:53:37 +08:00
|
|
|
|
2020-09-02 18:09:03 +08:00
|
|
|
return __ccs_read_addr(sensor, reg, val,
|
|
|
|
ccs_needs_quirk(sensor,
|
2020-09-02 18:23:40 +08:00
|
|
|
CCS_QUIRK_FLAG_8BIT_READ_ONLY),
|
|
|
|
conv);
|
2014-04-01 19:37:38 +08:00
|
|
|
}
|
|
|
|
|
2020-02-11 21:19:13 +08:00
|
|
|
int ccs_read_addr(struct ccs_sensor *sensor, u32 reg, u32 *val)
|
2012-04-22 20:11:26 +08:00
|
|
|
{
|
2020-09-02 18:23:40 +08:00
|
|
|
return ccs_read_addr_raw(sensor, reg, val, false, true, true);
|
2014-12-14 04:53:37 +08:00
|
|
|
}
|
2014-04-01 19:37:38 +08:00
|
|
|
|
2020-02-11 21:19:13 +08:00
|
|
|
int ccs_read_addr_8only(struct ccs_sensor *sensor, u32 reg, u32 *val)
|
2014-12-14 04:53:37 +08:00
|
|
|
{
|
2020-09-02 18:23:40 +08:00
|
|
|
return ccs_read_addr_raw(sensor, reg, val, true, true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ccs_read_addr_noconv(struct ccs_sensor *sensor, u32 reg, u32 *val)
|
|
|
|
{
|
|
|
|
return ccs_read_addr_raw(sensor, reg, val, false, true, false);
|
2012-04-22 20:11:26 +08:00
|
|
|
}
|
|
|
|
|
2020-02-11 21:19:13 +08:00
|
|
|
int ccs_write_addr_no_quirk(struct ccs_sensor *sensor, u32 reg, u32 val)
|
2012-03-04 04:19:52 +08:00
|
|
|
{
|
2012-04-22 19:55:10 +08:00
|
|
|
struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
|
2012-03-04 04:19:52 +08:00
|
|
|
struct i2c_msg msg;
|
|
|
|
unsigned char data[6];
|
|
|
|
unsigned int retries;
|
2020-02-03 19:38:42 +08:00
|
|
|
unsigned int len = ccs_reg_width(reg);
|
2012-03-04 04:19:52 +08:00
|
|
|
int r;
|
|
|
|
|
2020-02-03 19:36:55 +08:00
|
|
|
if (len > sizeof(data) - 2)
|
2012-03-04 04:19:52 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
msg.addr = client->addr;
|
|
|
|
msg.flags = 0; /* Write */
|
|
|
|
msg.len = 2 + len;
|
|
|
|
msg.buf = data;
|
|
|
|
|
2020-02-11 21:19:13 +08:00
|
|
|
put_unaligned_be16(CCS_REG_ADDR(reg), data);
|
2020-02-03 19:36:55 +08:00
|
|
|
put_unaligned_be32(val << (8 * (sizeof(val) - len)), data + 2);
|
2012-03-04 04:19:52 +08:00
|
|
|
|
2020-11-13 23:07:28 +08:00
|
|
|
for (retries = 0; retries < 10; retries++) {
|
2012-03-04 04:19:52 +08:00
|
|
|
/*
|
|
|
|
* Due to unknown reason sensor stops responding. This
|
|
|
|
* loop is a temporaty solution until the root cause
|
|
|
|
* is found.
|
|
|
|
*/
|
|
|
|
r = i2c_transfer(client->adapter, &msg, 1);
|
|
|
|
if (r == 1) {
|
|
|
|
if (retries)
|
|
|
|
dev_err(&client->dev,
|
[media] smiapp: don't break long lines
Due to the 80-cols restrictions, and latter due to checkpatch
warnings, several strings were broken into multiple lines. This
is not considered a good practice anymore, as it makes harder
to grep for strings at the source code.
As we're right now fixing other drivers due to KERN_CONT, we need
to be able to identify what printk strings don't end with a "\n".
It is a way easier to detect those if we don't break long lines.
So, join those continuation lines.
The patch was generated via the script below, and manually
adjusted if needed.
</script>
use Text::Tabs;
while (<>) {
if ($next ne "") {
$c=$_;
if ($c =~ /^\s+\"(.*)/) {
$c2=$1;
$next =~ s/\"\n$//;
$n = expand($next);
$funpos = index($n, '(');
$pos = index($c2, '",');
if ($funpos && $pos > 0) {
$s1 = substr $c2, 0, $pos + 2;
$s2 = ' ' x ($funpos + 1) . substr $c2, $pos + 2;
$s2 =~ s/^\s+//;
$s2 = ' ' x ($funpos + 1) . $s2 if ($s2 ne "");
print unexpand("$next$s1\n");
print unexpand("$s2\n") if ($s2 ne "");
} else {
print "$next$c2\n";
}
$next="";
next;
} else {
print $next;
}
$next="";
} else {
if (m/\"$/) {
if (!m/\\n\"$/) {
$next=$_;
next;
}
}
}
print $_;
}
</script>
Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
2016-10-19 03:44:01 +08:00
|
|
|
"sensor i2c stall encountered. retries: %d\n",
|
|
|
|
retries);
|
2012-03-04 04:19:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-13 23:07:28 +08:00
|
|
|
usleep_range(1000, 2000);
|
2012-03-04 04:19:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dev_err(&client->dev,
|
2020-02-03 19:36:55 +08:00
|
|
|
"wrote 0x%x to offset 0x%x error %d\n", val,
|
2020-02-11 21:19:13 +08:00
|
|
|
CCS_REG_ADDR(reg), r);
|
2012-03-04 04:19:52 +08:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2014-04-01 19:37:38 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write to a 8/16-bit register.
|
|
|
|
* Returns zero if successful, or non-zero otherwise.
|
|
|
|
*/
|
2020-02-11 21:19:13 +08:00
|
|
|
int ccs_write_addr(struct ccs_sensor *sensor, u32 reg, u32 val)
|
2014-04-01 19:37:38 +08:00
|
|
|
{
|
|
|
|
int rval;
|
|
|
|
|
2020-02-11 21:19:13 +08:00
|
|
|
rval = ccs_call_quirk(sensor, reg_access, true, ®, &val);
|
2014-04-01 19:37:38 +08:00
|
|
|
if (rval == -ENOIOCTLCMD)
|
|
|
|
return 0;
|
|
|
|
if (rval < 0)
|
|
|
|
return rval;
|
|
|
|
|
2020-02-11 19:16:14 +08:00
|
|
|
return ccs_write_addr_no_quirk(sensor, reg, val);
|
2014-04-01 19:37:38 +08:00
|
|
|
}
|