Coverage Report

Created: 2026-10-17 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/drivers/i2c/i2c.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * Copyright (C) 2009 Marc Kleine-Budde <mkl@pengutronix.de>
4
 *
5
 * Derived from:
6
 * - i2c-core.c - a device driver for the iic-bus interface
7
 *   Copyright (C) 1995-99 Simon G. Vogl
8
 * - at24.c - handle most I2C EEPROMs
9
 *   Copyright (C) 2005-2007 David Brownell
10
 *   Copyright (C) 2008 Wolfram Sang, Pengutronix
11
 * - spi.c - barebox SPI Framework
12
 *   Copyright (C) 2008 Sascha Hauer, Pengutronix
13
 * - Linux SPI Framework
14
 *   Copyright (C) 2005 David Brownell
15
 *
16
 */
17
18
#include <clock.h>
19
#include <common.h>
20
#include <errno.h>
21
#include <malloc.h>
22
#include <xfuncs.h>
23
#include <init.h>
24
#include <of.h>
25
#include <gpio.h>
26
#include <slice.h>
27
28
#include <i2c/i2c.h>
29
30
/**
31
 * I2C devices should normally not be created by I2C device drivers;
32
 * that would make them board-specific. Similarly with I2C master
33
 * drivers. Device registration normally goes into like
34
 * arch/.../mach.../board-YYY.c with other readonly (flashable)
35
 * information about mainboard devices.
36
 */
37
struct boardinfo {
38
  struct list_head  list;
39
  unsigned int    bus_num;
40
  unsigned int    n_board_info;
41
  struct i2c_board_info board_info[0];
42
};
43
44
static LIST_HEAD(board_list);
45
DEFINE_DEV_CLASS(i2c_adapter_class, "i2c_adapter");
46
47
/**
48
 * i2c_transfer - execute a single or combined I2C message
49
 * @param adap  Handle to I2C bus
50
 * @param msgs  One or more messages to execute before STOP is
51
 *      issued to terminate the operation; each
52
 *      message begins with a START.
53
 *
54
 * @param num Number of messages to be executed.
55
 *
56
 * Returns negative errno, else the number of messages executed.
57
 *
58
 * Note that there is no requirement that each message be sent to the
59
 * same slave address, although that is the most common model.
60
 */
61
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
62
0
{
63
0
  uint64_t start;
64
0
  int ret, try;
65
66
0
  slice_acquire(&adap->slice);
67
68
  /*
69
   * REVISIT the fault reporting model here is weak:
70
   *
71
   *  - When we get an error after receiving N bytes from a slave,
72
   *    there is no way to report "N".
73
   *
74
   *  - When we get a NAK after transmitting N bytes to a slave,
75
   *    there is no way to report "N" ... or to let the master
76
   *    continue executing the rest of this combined message, if
77
   *    that's the appropriate response.
78
   *
79
   *  - When for example "num" is two and we successfully complete
80
   *    the first message but get an error part way through the
81
   *    second, it's unclear whether that should be reported as
82
   *    one (discarding status on the second message) or errno
83
   *    (discarding status on the first one).
84
   */
85
86
0
  for (ret = 0; ret < num; ret++) {
87
0
    dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
88
0
      "len=%d\n", ret, (msgs[ret].flags & I2C_M_RD)
89
0
      ? 'R' : 'W', msgs[ret].addr, msgs[ret].len);
90
0
  }
91
92
  /* Retry automatically on arbitration loss */
93
0
  start = get_time_ns();
94
0
  for (ret = 0, try = 0; try <= 2; try++) {
95
0
    ret = adap->master_xfer(adap, msgs, num);
96
0
    if (ret != -EAGAIN)
97
0
      break;
98
0
    if (is_timeout(start, SECOND >> 1))
99
0
      break;
100
0
  }
101
102
0
  slice_release(&adap->slice);
103
104
0
  return ret;
105
0
}
106
EXPORT_SYMBOL(i2c_transfer);
107
108
/**
109
 * i2c_master_send - issue a single I2C message in master transmit mode
110
 *
111
 * @param client  Handle to slave device
112
 * @param buf Data that will be written to the slave
113
 * @param count How many bytes to write
114
 *
115
 * Returns negative errno, or else the number of bytes written.
116
 */
117
int i2c_master_send(struct i2c_client *client, const char *buf, int count)
118
0
{
119
0
  struct i2c_adapter *adap = client->adapter;
120
0
  struct i2c_msg msg;
121
0
  int ret;
122
123
0
  msg.addr = client->addr;
124
0
  msg.flags = 0;
125
0
  msg.len = count;
126
0
  msg.buf = (char *)buf;
127
128
0
  ret = i2c_transfer(adap, &msg, 1);
129
130
  /*
131
   * If everything went ok (i.e. 1 msg transmitted), return
132
   * #bytes transmitted, else error code.
133
   */
134
0
  return (ret == 1) ? count : ret;
135
0
}
136
EXPORT_SYMBOL(i2c_master_send);
137
138
/**
139
 * i2c_master_recv - issue a single I2C message in master receive mode
140
 *
141
 * @param client  Handle to slave device
142
 * @param buf Where to store data read from slave
143
 * @param count How many bytes to read
144
 *
145
 * Returns negative errno, or else the number of bytes read.
146
 */
147
int i2c_master_recv(struct i2c_client *client, char *buf, int count)
148
0
{
149
0
  struct i2c_adapter *adap = client->adapter;
150
0
  struct i2c_msg msg;
151
0
  int ret;
152
153
0
  msg.addr = client->addr;
154
0
  msg.flags = I2C_M_RD;
155
0
  msg.len = count;
156
0
  msg.buf = buf;
157
158
0
  ret = i2c_transfer(adap, &msg, 1);
159
160
  /*
161
   * If everything went ok (i.e. 1 msg transmitted), return
162
   * #bytes transmitted, else error code.
163
   */
164
0
  return (ret == 1) ? count : ret;
165
0
}
166
EXPORT_SYMBOL(i2c_master_recv);
167
168
int i2c_read_reg(struct i2c_client *client, u32 addr, u8 *buf, u16 count)
169
0
{
170
0
  u8 msgbuf[2];
171
0
  struct i2c_msg msg[] = {
172
0
    {
173
0
      .addr = client->addr,
174
0
      .buf  = msgbuf,
175
0
    },
176
0
    {
177
0
      .addr = client->addr,
178
0
      .flags  = I2C_M_RD,
179
0
      .buf  = buf,
180
0
      .len  = count,
181
0
    },
182
0
  };
183
0
  int status, i;
184
185
0
  i = 0;
186
0
  if (addr & I2C_ADDR_16_BIT)
187
0
    msgbuf[i++] = addr >> 8;
188
0
  msgbuf[i++] = addr;
189
0
  msg->len = i;
190
191
0
  status = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
192
0
  dev_dbg(&client->adapter->dev, "%s: %u@%u --> %d\n", __func__,
193
0
    count, addr, status);
194
195
0
  if (status == ARRAY_SIZE(msg))
196
0
    return count;
197
0
  else if (status >= 0)
198
0
    return -EIO;
199
0
  else
200
0
    return status;
201
0
}
202
EXPORT_SYMBOL(i2c_read_reg);
203
204
int i2c_write_reg(struct i2c_client *client, u32 addr, const u8 *buf, u16 count)
205
0
{
206
0
  u8 msgbuf[256];       /* FIXME */
207
0
  struct i2c_msg msg[] = {
208
0
    {
209
0
      .addr = client->addr,
210
0
      .buf  = msgbuf,
211
0
      .len  = count,
212
0
    }
213
0
  };
214
0
  int status, i;
215
216
0
  i = 0;
217
0
  if (addr & I2C_ADDR_16_BIT)
218
0
    msgbuf[i++] = addr >> 8;
219
0
  msgbuf[i++] = addr;
220
0
  msg->len += i;
221
222
0
  if (count)
223
0
    memcpy(msg->buf + i, buf, count);
224
225
0
  status = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
226
0
  dev_dbg(&client->adapter->dev, "%s: %u@%u --> %d\n", __func__,
227
0
    count, addr, status);
228
229
0
  if (status == ARRAY_SIZE(msg))
230
0
    return count;
231
0
  else if (status >= 0)
232
0
    return -EIO;
233
0
  else
234
0
    return status;
235
0
}
236
EXPORT_SYMBOL(i2c_write_reg);
237
238
/* i2c bus recovery routines */
239
int i2c_get_scl_gpio_value(struct i2c_adapter *adap)
240
0
{
241
0
  gpio_direction_input(adap->bus_recovery_info->scl_gpio);
242
0
  return gpio_get_value(adap->bus_recovery_info->scl_gpio);
243
0
}
244
245
void i2c_set_scl_gpio_value(struct i2c_adapter *adap, int val)
246
0
{
247
0
  if (val)
248
0
    gpio_direction_input(adap->bus_recovery_info->scl_gpio);
249
0
  else
250
0
    gpio_direction_output(adap->bus_recovery_info->scl_gpio, 0);
251
0
}
252
253
int i2c_get_sda_gpio_value(struct i2c_adapter *adap)
254
0
{
255
0
  return gpio_get_value(adap->bus_recovery_info->sda_gpio);
256
0
}
257
258
static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
259
0
{
260
0
  struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
261
0
  struct device *dev = &adap->dev;
262
0
  int ret = 0;
263
264
0
  ret = gpio_request_one(bri->scl_gpio, GPIOF_IN, "i2c-scl");
265
0
  if (ret) {
266
0
    dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
267
0
    return ret;
268
0
  }
269
270
0
  if (bri->get_sda) {
271
0
    if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
272
      /* work without SDA polling */
273
0
      dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
274
0
          bri->sda_gpio);
275
0
      bri->get_sda = NULL;
276
0
    }
277
0
  }
278
279
0
  return ret;
280
0
}
281
282
static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
283
0
{
284
0
  struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
285
286
0
  if (bri->get_sda)
287
0
    gpio_free(bri->sda_gpio);
288
289
0
  gpio_free(bri->scl_gpio);
290
0
}
291
292
/*
293
 * We are generating clock pulses. ndelay() determines durating of clk pulses.
294
 * We will generate clock with rate 100 KHz and so duration of both clock levels
295
 * is: delay in ns = (10^6 / 100) / 2
296
 */
297
0
#define RECOVERY_NDELAY   5000
298
0
#define RECOVERY_CLK_CNT  9
299
300
static int i2c_generic_recovery(struct i2c_adapter *adap)
301
0
{
302
0
  struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
303
0
  int i = 0, val = 1, ret = 0;
304
305
0
  if (bri->prepare_recovery)
306
0
    bri->prepare_recovery(adap);
307
308
0
  bri->set_scl(adap, val);
309
0
  ndelay(RECOVERY_NDELAY);
310
311
  /*
312
   * By this time SCL is high, as we need to give 9 falling-rising edges
313
   */
314
0
  while (i++ < RECOVERY_CLK_CNT * 2) {
315
0
    if (val) {
316
      /* Break if SDA is high */
317
0
      if (bri->get_sda && bri->get_sda(adap))
318
0
          break;
319
      /* SCL shouldn't be low here */
320
0
      if (!bri->get_scl(adap)) {
321
0
        dev_err(&adap->dev,
322
0
          "SCL is stuck low, exit recovery\n");
323
0
        ret = -EBUSY;
324
0
        break;
325
0
      }
326
0
    }
327
328
0
    val = !val;
329
0
    bri->set_scl(adap, val);
330
0
    ndelay(RECOVERY_NDELAY);
331
0
  }
332
333
0
  if (bri->unprepare_recovery)
334
0
    bri->unprepare_recovery(adap);
335
336
0
  return ret;
337
0
}
338
339
int i2c_generic_scl_recovery(struct i2c_adapter *adap)
340
0
{
341
0
  return i2c_generic_recovery(adap);
342
0
}
343
344
int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
345
0
{
346
0
  int ret;
347
348
0
  ret = i2c_get_gpios_for_recovery(adap);
349
0
  if (ret)
350
0
    return ret;
351
352
0
  ret = i2c_generic_recovery(adap);
353
0
  i2c_put_gpios_for_recovery(adap);
354
355
0
  return ret;
356
0
}
357
358
int i2c_recover_bus(struct i2c_adapter *adap)
359
0
{
360
0
  if (!adap->bus_recovery_info)
361
0
    return -EBUSY;
362
363
0
  dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
364
0
  return adap->bus_recovery_info->recover_bus(adap);
365
0
}
366
367
static void i2c_info(struct device *dev)
368
0
{
369
0
  const struct i2c_client *client = to_i2c_client(dev);
370
371
0
  printf("  Address: 0x%02x\n", client->addr);
372
0
  return;
373
0
}
374
375
/**
376
 * i2c_new_device - instantiate one new I2C device
377
 *
378
 * @param adapter Controller to which device is connected
379
 * @param chip  Describes the I2C device
380
 *
381
 * On typical mainboards, this is purely internal; and it's not needed
382
 * after board init creates the hard-wired devices. Some development
383
 * platforms may not be able to use i2c_register_board_info though,
384
 * and this is exported so that for example a USB or parport based
385
 * adapter driver could add devices (which it would learn about
386
 * out-of-band).
387
 *
388
 * Returns the new device, or NULL.
389
 */
390
static struct i2c_client *i2c_new_device(struct i2c_adapter *adapter,
391
           struct i2c_board_info *chip)
392
0
{
393
0
  struct i2c_client *client;
394
0
  int status;
395
396
0
  client = xzalloc(sizeof *client);
397
0
  dev_set_name(&client->dev, "%s", chip->type);
398
0
  client->dev.type_data = client;
399
0
  client->dev.platform_data = chip->platform_data;
400
0
  client->dev.id = DEVICE_ID_DYNAMIC;
401
0
  client->dev.bus = &i2c_bus;
402
0
  client->dev.of_node = chip->of_node;
403
0
  client->adapter = adapter;
404
0
  client->addr = chip->addr;
405
406
0
  client->dev.parent = &adapter->dev;
407
408
0
  status = register_device(&client->dev);
409
0
  if (status) {
410
0
    free(client);
411
0
    return NULL;
412
0
  }
413
0
  devinfo_add(&client->dev, i2c_info);
414
415
0
  if (chip->of_node)
416
0
    chip->of_node->dev = &client->dev;
417
418
0
  dev_dbg(&client->dev, "registered on bus %d, chip->addr 0x%02x\n",
419
0
    adapter->nr, client->addr);
420
421
0
  return client;
422
0
}
423
424
void i2c_unregister_device(struct i2c_client *client)
425
0
{
426
0
  unregister_device(&client->dev);
427
0
  devinfo_del(&client->dev, i2c_info);
428
0
}
429
430
static void of_i2c_register_devices(struct i2c_adapter *adap)
431
0
{
432
0
  struct device_node *n;
433
434
  /* Only register child devices if the adapter has a node pointer set */
435
0
  if (!IS_ENABLED(CONFIG_OFDEVICE) || !adap->dev.of_node)
436
0
    return;
437
438
0
  for_each_available_child_of_node(adap->dev.of_node, n) {
439
0
    struct i2c_board_info info = {};
440
0
    struct i2c_client *result;
441
0
    const __be32 *addr;
442
0
    int len;
443
444
0
    if (n->dev) {
445
0
      dev_dbg(&adap->dev, "of_i2c: skipping already registered %s\n",
446
0
        dev_name(n->dev));
447
0
      continue;
448
0
    }
449
450
0
    of_modalias_node(n, info.type, I2C_NAME_SIZE);
451
452
0
    info.of_node = n;
453
454
0
    addr = of_get_property(n, "reg", &len);
455
0
    if (!addr || (len < sizeof(int))) {
456
0
      dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", n);
457
0
      continue;
458
0
    }
459
460
0
    info.addr = be32_to_cpup(addr);
461
0
    if (info.addr > (1 << 10) - 1) {
462
0
      dev_err(&adap->dev, "of_i2c: invalid addr=%x on %pOF\n",
463
0
        info.addr, n);
464
0
      continue;
465
0
    }
466
467
0
    result = i2c_new_device(adap, &info);
468
0
    if (!result)
469
0
      dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n",
470
0
              n);
471
0
  }
472
0
}
473
474
int of_i2c_register_devices_by_node(struct device_node *node)
475
0
{
476
0
  struct i2c_adapter *adap;
477
478
0
  adap = of_find_i2c_adapter_by_node(node);
479
0
  if (!adap)
480
0
    return -ENODEV;
481
0
  if (IS_ERR(adap))
482
0
    return PTR_ERR(adap);
483
484
0
  of_i2c_register_devices(adap);
485
0
  return 0;
486
0
}
487
488
static void i2c_hw_rescan(struct device *dev)
489
0
{
490
0
  struct i2c_adapter *adap;
491
492
0
  for_each_i2c_adapter(adap) {
493
0
    if (dev != adap->dev.parent)
494
0
      continue;
495
0
    of_i2c_register_devices(adap);
496
0
    break;
497
0
  }
498
0
}
499
500
/**
501
 * i2c_new_dummy - return a new i2c device bound to a dummy driver
502
 * @adapter: the adapter managing the device
503
 * @address: seven bit address to be used
504
 * Context: can sleep
505
 *
506
 * This returns an I2C client bound to the "dummy" driver, intended for use
507
 * with devices that consume multiple addresses.  Examples of such chips
508
 * include various EEPROMS (like 24c04 and 24c08 models).
509
 *
510
 * These dummy devices have two main uses.  First, most I2C and SMBus calls
511
 * except i2c_transfer() need a client handle; the dummy will be that handle.
512
 * And second, this prevents the specified address from being bound to a
513
 * different driver.
514
 *
515
 * This returns the new i2c client, which should be saved for later use with
516
 * i2c_unregister_device(); or NULL to indicate an error.
517
 */
518
struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
519
0
{
520
0
  struct i2c_board_info info = {
521
0
    I2C_BOARD_INFO("dummy", address),
522
0
  };
523
524
0
  return i2c_new_device(adapter, &info);
525
0
}
526
EXPORT_SYMBOL_GPL(i2c_new_dummy);
527
528
/**
529
 * i2c_register_board_info - register I2C devices for a given board
530
 *
531
 * @param info  array of chip descriptors
532
 * @param n how many descriptors are provided
533
 *
534
 * Board-specific early init code calls this (probably during
535
 * arch_initcall) with segments of the I2C device table.
536
 *
537
 * Other code can also call this, e.g. a particular add-on board might
538
 * provide I2C devices through its expansion connector, so code
539
 * initializing that board would naturally declare its I2C devices.
540
 *
541
 */
542
int i2c_register_board_info(int bus_num, struct i2c_board_info const *info, unsigned n)
543
0
{
544
0
  struct boardinfo *bi;
545
546
0
  bi = xmalloc(sizeof(*bi) + n * sizeof(*info));
547
548
0
  bi->n_board_info = n;
549
0
  bi->bus_num = bus_num;
550
0
  memcpy(bi->board_info, info, n * sizeof(*info));
551
552
0
  list_add_tail(&bi->list, &board_list);
553
554
0
  return 0;
555
0
}
556
557
static void scan_boardinfo(struct i2c_adapter *adapter)
558
0
{
559
0
  struct boardinfo  *bi;
560
561
0
  list_for_each_entry(bi, &board_list, list) {
562
0
    struct i2c_board_info *chip = bi->board_info;
563
0
    unsigned n;
564
565
0
    if (bi->bus_num != adapter->nr)
566
0
      continue;
567
568
0
    for (n = bi->n_board_info; n > 0; n--, chip++) {
569
      /*
570
       * NOTE: this relies on i2c_new_device to
571
       * issue diagnostics when given bogus inputs
572
       */
573
0
      (void) i2c_new_device(adapter, chip);
574
0
    }
575
0
  }
576
0
}
577
578
/**
579
 *
580
 * i2c_get_adapter - get an i2c adapter from its busnum
581
 *
582
 * @param busnum  the desired bus number
583
 *
584
 */
585
struct i2c_adapter *i2c_get_adapter(int busnum)
586
0
{
587
0
  struct i2c_adapter *adap;
588
589
0
  for_each_i2c_adapter(adap)
590
0
    if (adap->nr == busnum)
591
0
      return adap;
592
0
  return NULL;
593
0
}
594
595
struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
596
0
{
597
0
  struct i2c_adapter *adap;
598
0
  int ret;
599
600
0
  ret = of_device_ensure_probed(node);
601
0
  if (ret)
602
0
    return NULL;
603
604
0
  for_each_i2c_adapter(adap)
605
0
    if (adap->dev.of_node == node)
606
0
      return adap;
607
608
0
  return NULL;
609
0
}
610
611
struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
612
0
{
613
0
  struct device *dev = of_find_device_by_node(node);
614
615
0
  if (!dev)
616
0
    return NULL;
617
618
0
  if (dev->bus != &i2c_bus)
619
0
    return NULL;
620
621
0
  return to_i2c_client(dev);
622
0
}
623
624
int of_i2c_device_enable_and_register_by_alias(const char *alias)
625
0
{
626
0
  struct device_node *np;
627
628
0
  np = of_find_node_by_alias(NULL, alias);
629
0
  if (!np)
630
0
    return -ENODEV;
631
632
0
  of_device_enable(np);
633
0
  return of_i2c_register_devices_by_node(np->parent);
634
0
}
635
636
637
static void i2c_parse_timing(struct device *dev, char *prop_name,
638
           u32 *cur_val_p,
639
           u32 def_val, bool use_def)
640
0
{
641
0
  int ret;
642
643
0
  ret = of_property_read_u32(dev->of_node, prop_name, cur_val_p);
644
0
  if (ret && use_def)
645
0
    *cur_val_p = def_val;
646
647
0
  dev_dbg(dev, "%s: %u\n", prop_name, *cur_val_p);
648
0
}
649
650
/**
651
 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
652
 * @dev: The device to scan for I2C timing properties
653
 * @t: the i2c_timings struct to be filled with values
654
 * @use_defaults: bool to use sane defaults derived from the I2C specification
655
 *      when properties are not found, otherwise use 0
656
 *
657
 * Scan the device for the generic I2C properties describing timing parameters
658
 * for the signal and fill the given struct with the results. If a property was
659
 * not found and use_defaults was true, then maximum timings are assumed which
660
 * are derived from the I2C specification. If use_defaults is not used, the
661
 * results will be 0, so drivers can apply their own defaults later. The latter
662
 * is mainly intended for avoiding regressions of existing drivers which want
663
 * to switch to this function. New drivers almost always should use the defaults.
664
 */
665
666
void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t,
667
        bool use_defaults)
668
0
{
669
0
  bool u = use_defaults;
670
0
  u32 d;
671
672
0
  i2c_parse_timing(dev, "clock-frequency", &t->bus_freq_hz,
673
0
       I2C_MAX_STANDARD_MODE_FREQ, u);
674
675
0
  d = t->bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ ? 1000 :
676
0
      t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120;
677
0
  i2c_parse_timing(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns, d, u);
678
679
0
  d = t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120;
680
0
  i2c_parse_timing(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns, d, u);
681
682
0
  i2c_parse_timing(dev, "i2c-scl-internal-delay-ns",
683
0
       &t->scl_int_delay_ns, 0, u);
684
0
  i2c_parse_timing(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns,
685
0
       t->scl_fall_ns, u);
686
0
  i2c_parse_timing(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns, 0, u);
687
0
  i2c_parse_timing(dev, "i2c-digital-filter-width-ns",
688
0
       &t->digital_filter_width_ns, 0, u);
689
0
  i2c_parse_timing(dev, "i2c-analog-filter-cutoff-frequency",
690
0
       &t->analog_filter_cutoff_freq_hz, 0, u);
691
0
}
692
EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
693
694
/**
695
 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
696
 * @adapter: the adapter to register (with adap->nr initialized)
697
 *
698
 * This routine is used to declare an I2C adapter when its bus number
699
 * matters.  For example, use it for I2C adapters from system-on-chip CPUs,
700
 * or otherwise built in to the system's mainboard, and where i2c_board_info
701
 * is used to properly configure I2C devices.
702
 *
703
 * When this returns zero, the specified adapter became available for
704
 * clients using the bus number provided in adap->nr.  Also, the table
705
 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
706
 * and the appropriate driver model device nodes are created.  Otherwise, a
707
 * negative errno value is returned.
708
 */
709
int i2c_add_numbered_adapter(struct i2c_adapter *adapter)
710
0
{
711
0
  struct device *hw_dev;
712
0
  int ret;
713
714
0
  if (adapter->nr < 0 && dev_of_node(&adapter->dev))
715
0
    adapter->nr = of_alias_get_id(adapter->dev.of_node, "i2c");
716
717
0
  if (adapter->nr < 0) {
718
0
    int nr;
719
720
0
    for (nr = of_alias_get_free_id("i2c");
721
0
         i2c_get_adapter(nr); nr++)
722
0
      ;
723
724
0
    adapter->nr = nr;
725
0
  } else {
726
0
    if (i2c_get_adapter(adapter->nr))
727
0
      return -EBUSY;
728
0
  }
729
730
0
  adapter->dev.id = adapter->nr;
731
0
  dev_set_name(&adapter->dev, "i2c");
732
733
0
  ret = register_device(&adapter->dev);
734
0
  if (ret)
735
0
    return ret;
736
737
0
  class_add_device(&i2c_adapter_class, &adapter->dev);
738
739
0
  slice_init(&adapter->slice, dev_name(&adapter->dev));
740
741
  /* populate children from any i2c device tables */
742
0
  scan_boardinfo(adapter);
743
744
0
  of_i2c_register_devices(adapter);
745
746
0
  hw_dev = adapter->dev.parent;
747
0
  if (hw_dev && dev_of_node(hw_dev)) {
748
0
    if (!hw_dev->rescan)
749
0
      hw_dev->rescan = i2c_hw_rescan;
750
0
  }
751
752
0
  return 0;
753
0
}
754
EXPORT_SYMBOL(i2c_add_numbered_adapter);
755
756
struct bus_type i2c_bus = {
757
  .name = "i2c",
758
  .match = device_match_of_modalias,
759
};
760
EXPORT_SYMBOL(i2c_bus);
761
762
static int i2c_bus_init(void)
763
26
{
764
26
  return bus_register(&i2c_bus);
765
26
}
766
pure_initcall(i2c_bus_init);