Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
Move loop indexes into loops
Return on errors instead of loop breaks
Some variables cleanup
  • Loading branch information
klogg committed May 12, 2024
1 parent 7c2a33e commit ed5a85b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 65 deletions.
2 changes: 1 addition & 1 deletion bridge/it66121.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "it66121_registers.h"

#define UNUSED(x) (void)(x)
#define UNUSED(x) ((void)(x))

/* HW defect leading to losing 3 first bytes during EDID read operation */
#define EDID_LOSS_LEN 3
Expand Down
40 changes: 22 additions & 18 deletions bridge/it66121_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ static int it66121_configure_afe(struct it66121_priv *priv, int clock_khz)

static int it66121_wait_ddc_ready(struct it66121_priv *priv)
{
int ret, val;
int ret;
unsigned int val;

ret = regmap_field_read_poll_timeout(priv->ddc_done, val, true, EDID_SLEEP, EDID_TIMEOUT);
if (ret)
Expand Down Expand Up @@ -221,7 +222,7 @@ static int it66121_clear_ddc_fifo(struct it66121_priv *priv)

static int it66121_abort_ddc_ops(struct it66121_priv *priv)
{
int i, ret;
int ret;

/* Prior to DDC abort command there was also a reset of HDCP:
* 1. HDCP_DESIRE clear bit CP DESIRE
Expand All @@ -233,7 +234,7 @@ static int it66121_abort_ddc_ops(struct it66121_priv *priv)
/* From original driver: According to 2009/01/15 modification by [email protected]
* do abort DDC twice
*/
for (i = 0; i < 2; i++) {
for (int i = 0; i < 2; i++) {
ret = regmap_write(priv->regmap, IT66121_DDC_COMMAND, DDC_CMD_ABORT);
if (ret)
return ret;
Expand Down Expand Up @@ -326,9 +327,11 @@ static void it66121_intr_work(struct work_struct *work_item)

static int it66121_get_edid_block(void *context, u8 *buf, unsigned int block, size_t len)
{
int i, ret, offset = block & 1 ? 128 : 0;
int ret;
size_t remain = len;
unsigned int rd_fifo_val, segment = block >> 1;
unsigned int val;
unsigned int segment = block >> 1;
unsigned int offset = block & 1 ? 128 : 0;
static const u8 header[EDID_LOSS_LEN] = { 0x00, 0xFF, 0xFF };
struct it66121_priv *priv = context;

Expand All @@ -354,35 +357,36 @@ static int it66121_get_edid_block(void *context, u8 *buf, unsigned int block, si
/* Clear DDC FIFO */
ret = it66121_clear_ddc_fifo(priv);
if (ret)
break;
return ret;

ret = regmap_write(priv->regmap, IT66121_DDC_ADDRESS, EDID_DDC_ADDR);
if (ret)
break;
return ret;

/* Account 3 bytes that will be lost */
ret = regmap_write(priv->regmap, IT66121_DDC_OFFSET, offset - EDID_LOSS_LEN);
if (ret)
break;
return ret;

ret = regmap_write(priv->regmap, IT66121_DDC_SIZE, (unsigned int)size);
if (ret)
break;
return ret;
ret = regmap_write(priv->regmap, IT66121_DDC_SEGMENT, segment);
if (ret)
break;
return ret;
ret = regmap_write(priv->regmap, IT66121_DDC_COMMAND, DDC_CMD_EDID_READ);
if (ret)
break;
return ret;

/* Deduct lost bytes when reading from FIFO */
size -= EDID_LOSS_LEN;

for (i = 0; i < size; i++) {
ret = regmap_read(priv->regmap, IT66121_DDC_RD_FIFO, &rd_fifo_val);
for (int i = 0; i < size; i++) {
ret = regmap_read(priv->regmap, IT66121_DDC_RD_FIFO, &val);
if (ret)
return ret;

*(buf++) = rd_fifo_val & 0xFF;
*(buf++) = val & 0xFF;
}

remain -= size;
Expand Down Expand Up @@ -601,7 +605,7 @@ static void it66121_bridge_mode_set(struct drm_bridge *bridge, const struct drm_
const struct drm_display_mode *adjusted_mode)

{
int i, ret;
int ret;
ssize_t frame_size;
struct it66121_priv *priv = container_of(bridge, struct it66121_priv, bridge);
u8 buf[HDMI_INFOFRAME_SIZE(AVI)];
Expand Down Expand Up @@ -658,7 +662,7 @@ static void it66121_bridge_mode_set(struct drm_bridge *bridge, const struct drm_
}

/* Write new AVI infoframe packet */
for (i = 0; i < HDMI_AVI_INFOFRAME_SIZE; i++) {
for (int i = 0; i < HDMI_AVI_INFOFRAME_SIZE; i++) {
ret = regmap_write(priv->regmap, aviinfo_reg[i],
buf[i + HDMI_INFOFRAME_HEADER_SIZE]);
if (ret) {
Expand Down Expand Up @@ -764,7 +768,7 @@ static int it66121_regs_init(struct it66121_priv *priv, struct i2c_client *clien

static int it66121_i2c_probe(struct i2c_adapter *adapter, unsigned short address)
{
int i, ret;
int ret;
u8 id_regs[] = { IT66121_VENDOR_ID_1, IT66121_VENDOR_ID_2, IT66121_DEVICE_ID_1,
IT66121_DEVICE_ID_2 };
union {
Expand All @@ -784,7 +788,7 @@ static int it66121_i2c_probe(struct i2c_adapter *adapter, unsigned short address
return -ENODEV;
}

for (i = 0; i < ARRAY_SIZE(id_regs); i++) {
for (int i = 0; i < ARRAY_SIZE(id_regs); i++) {
struct i2c_msg msgs[] = {
{ .addr = address, .flags = 0, .len = 1, .buf = &id_regs[i] },
{ .addr = address, .flags = I2C_M_RD, .len = 1, .buf = &id.b[i] }
Expand Down
2 changes: 1 addition & 1 deletion fl2000.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

#include "fl2000_registers.h"

#define UNUSED(x) (void)(x)
#define UNUSED(x) ((void)(x))

/* Known USB interfaces of FL2000 */
enum fl2000_interface {
Expand Down
39 changes: 17 additions & 22 deletions fl2000_drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ static const u32 fl2000_pixel_formats[] = {

static unsigned int fl2000_get_bytes_pix(enum usb_device_speed speed, unsigned int pixclock)
{
unsigned int bytes_pix, max_bw;
unsigned int bytes_pix;
unsigned int max_bw;

/* Calculate maximum bandwidth, bytes per second */
switch (speed) {
Expand Down Expand Up @@ -151,12 +152,11 @@ static inline u32 fl2000_pll_get_divisor(u64 clock_mil, u32 vco_clk, u64 *min_pp
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128
};
unsigned int divisor_idx;
u32 best_divisor = 0;

/* Iterate over array */
for (divisor_idx = 0; divisor_idx < ARRAY_SIZE(divisor_arr); divisor_idx++) {
u32 divisor = divisor_arr[divisor_idx];
for (int i = 0; i < ARRAY_SIZE(divisor_arr); i++) {
u32 divisor = divisor_arr[i];
u64 ppm_err = fl2000_pll_ppm_err(clock_mil, vco_clk, divisor);

if (ppm_err < *min_ppm_err) {
Expand All @@ -173,12 +173,10 @@ static u64 fl2000_pll_calc(u64 clock_mil, struct fl2000_pll *pll, u32 *clock_cal
{
static const u32 prescaler_max = 2;
static const u32 multiplier_max = 128;
u32 prescaler;
u32 multiplier;
u64 min_ppm_err = (u64)(-1);

for (prescaler = 1; prescaler <= prescaler_max; prescaler++)
for (multiplier = 1; multiplier <= multiplier_max; multiplier++) {
for (u32 prescaler = 1; prescaler <= prescaler_max; prescaler++)
for (u32 multiplier = 1; multiplier <= multiplier_max; multiplier++) {
/* Do not need precision here yet, no 10^6 multiply */
u32 vco_clk = FL2000_XTAL / prescaler * multiplier;
u32 divisor;
Expand All @@ -193,19 +191,17 @@ static u64 fl2000_pll_calc(u64 clock_mil, struct fl2000_pll *pll, u32 *clock_cal
pll->prescaler = prescaler;
pll->multiplier = multiplier;
pll->divisor = divisor;
if (vco_clk < 125000000) {
if (vco_clk < 125000000)
pll->function = 0;
} else if (vco_clk < 250000000) {
else if (vco_clk < 250000000)
pll->function = 1;
} else if (vco_clk < 500000000) {
else if (vco_clk < 500000000)
pll->function = 2;
} else {
else
pll->function = 3;
}
*clock_calculated = vco_clk / divisor;
}

/* No exact PLL settings found for requested clock */
return min_ppm_err;
}

Expand All @@ -217,17 +213,15 @@ static int fl2000_mode_calc(const struct drm_display_mode *mode,
u64 clock_mil_adjusted;
const u64 clock_mil = (u64)mode->clock * 1000 * FL2000_PLL_PRECISION;
const int max_h_adjustment = 10;
int s, m;
int d = 0;

if (mode->clock * 1000 > FL2000_MAX_PIXCLOCK)
return -1;

/* Try to match pixel clock slightly adjusting htotal value */
for (m = 0, s = 0; m <= max_h_adjustment * 2; m++, s = -s) {
/* 0, -1, 1, -2, 2, -3, 3, -3, 4, -4, 5, -5, ... */
d += m * s;

/* Try to match pixel clock slightly adjusting htotal value, sequence is:
* 0, -1, 1, -2, 2, -3, 3, -3, 4, -4, 5, -5, ...
* Here, 's' is used for sign, 'm' is used for modulo, and 'd' is the adjustment value
*/
for (int m = 0, s = 0, d = 0; m <= max_h_adjustment * 2; m++, s = -s, d += m * s) {
/* Maximum pixel clock 1GHz, or 10^9Hz. Multiply by 10^6 we get 10^15Hz. Assume
* maximum htotal is 10000 pix (no way) we get 10^19 max value and using u64 which
* is 1.8*10^19 no overflow can occur. Assume all this was checked before
Expand Down Expand Up @@ -302,7 +296,8 @@ static void fl2000_display_disable(struct drm_simple_display_pipe *pipe)

static void fb2000_dirty(struct drm_framebuffer *fb, struct drm_rect *rect)
{
int idx, ret;
int ret;
int idx;
struct drm_device *drm = fb->dev;
struct fl2000_drm_if *drm_if = drm->dev_private;
struct drm_gem_dma_object *dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
Expand Down
3 changes: 1 addition & 2 deletions fl2000_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ static struct component_master_ops fl2000_master_ops = {

static int fl2000_compare(struct device *dev, void *data)
{
int i;
struct i2c_client *client = i2c_verify_client(dev);
static const char *const fl2000_supported_bridges[] = {
"it66121", /* IT66121 driver name*/
Expand All @@ -52,7 +51,7 @@ static int fl2000_compare(struct device *dev, void *data)
return 0;

/* Check this is a supported DRM bridge */
for (i = 0; i < ARRAY_SIZE(fl2000_supported_bridges); i++)
for (int i = 0; i < ARRAY_SIZE(fl2000_supported_bridges); i++)
if (!strncmp(fl2000_supported_bridges[i], client->name, sizeof(client->name)))
return 1; /* Must be not 0 for success */

Expand Down
3 changes: 2 additions & 1 deletion fl2000_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ static int fl2000_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, in
int ret;
bool read;
u16 addr;
u8 idx, offset;
u8 idx;
u8 offset;
union {
u32 w;
u8 b[4];
Expand Down
Loading

0 comments on commit ed5a85b

Please sign in to comment.