Blame view

drivers/of/of_i2c.c 2.42 KB
612212a3f   Jochen Friedrich   [POWERPC] i2c: OF...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  /*
   * OF helpers for the I2C API
   *
   * Copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
   *
   * Based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   */
  
  #include <linux/i2c.h>
4c60071c1   David Daney   of/mips: Cleanup ...
15
  #include <linux/irq.h>
612212a3f   Jochen Friedrich   [POWERPC] i2c: OF...
16
  #include <linux/of.h>
f40987b64   Robert P. J. Day   OpenFirmware: Inc...
17
  #include <linux/of_i2c.h>
9fd049927   Grant Likely   of/i2c: Generaliz...
18
  #include <linux/of_irq.h>
138decf83   Adrian Bunk   [POWERPC] drivers...
19
  #include <linux/module.h>
612212a3f   Jochen Friedrich   [POWERPC] i2c: OF...
20

9fd049927   Grant Likely   of/i2c: Generaliz...
21
  void of_i2c_register_devices(struct i2c_adapter *adap)
612212a3f   Jochen Friedrich   [POWERPC] i2c: OF...
22
23
24
  {
  	void *result;
  	struct device_node *node;
9fd049927   Grant Likely   of/i2c: Generaliz...
25
26
27
28
29
30
31
32
  	/* Only register child devices if the adapter has a node pointer set */
  	if (!adap->dev.of_node)
  		return;
  
  	dev_dbg(&adap->dev, "of_i2c: walking child nodes
  ");
  
  	for_each_child_of_node(adap->dev.of_node, node) {
612212a3f   Jochen Friedrich   [POWERPC] i2c: OF...
33
  		struct i2c_board_info info = {};
e6a437eba   Anton Vorontsov   of/i2c: Fill the ...
34
  		struct dev_archdata dev_ad = {};
337148812   Jeremy Kerr   of: assume big-en...
35
  		const __be32 *addr;
612212a3f   Jochen Friedrich   [POWERPC] i2c: OF...
36
  		int len;
9fd049927   Grant Likely   of/i2c: Generaliz...
37
38
39
40
41
42
43
  		dev_dbg(&adap->dev, "of_i2c: register %s
  ", node->full_name);
  
  		if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
  			dev_err(&adap->dev, "of_i2c: modalias failure on %s
  ",
  				node->full_name);
3f07af494   Grant Likely   of: adapt of_find...
44
  			continue;
9fd049927   Grant Likely   of/i2c: Generaliz...
45
  		}
3f07af494   Grant Likely   of: adapt of_find...
46

612212a3f   Jochen Friedrich   [POWERPC] i2c: OF...
47
  		addr = of_get_property(node, "reg", &len);
9fd049927   Grant Likely   of/i2c: Generaliz...
48
49
50
51
  		if (!addr || (len < sizeof(int))) {
  			dev_err(&adap->dev, "of_i2c: invalid reg on %s
  ",
  				node->full_name);
612212a3f   Jochen Friedrich   [POWERPC] i2c: OF...
52
53
  			continue;
  		}
337148812   Jeremy Kerr   of: assume big-en...
54
  		info.addr = be32_to_cpup(addr);
9fd049927   Grant Likely   of/i2c: Generaliz...
55
56
57
58
59
60
  		if (info.addr > (1 << 10) - 1) {
  			dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s
  ",
  				info.addr, node->full_name);
  			continue;
  		}
612212a3f   Jochen Friedrich   [POWERPC] i2c: OF...
61

9fd049927   Grant Likely   of/i2c: Generaliz...
62
63
  		info.irq = irq_of_parse_and_map(node, 0);
  		info.of_node = of_node_get(node);
e6a437eba   Anton Vorontsov   of/i2c: Fill the ...
64
  		info.archdata = &dev_ad;
020862648   David Daney   of/i2c: Fix reque...
65
  		request_module("%s%s", I2C_MODULE_PREFIX, info.type);
612212a3f   Jochen Friedrich   [POWERPC] i2c: OF...
66
67
68
  
  		result = i2c_new_device(adap, &info);
  		if (result == NULL) {
9fd049927   Grant Likely   of/i2c: Generaliz...
69
70
71
72
  			dev_err(&adap->dev, "of_i2c: Failure registering %s
  ",
  			        node->full_name);
  			of_node_put(node);
612212a3f   Jochen Friedrich   [POWERPC] i2c: OF...
73
74
75
76
77
  			irq_dispose_mapping(info.irq);
  			continue;
  		}
  	}
  }
9fd049927   Grant Likely   of/i2c: Generaliz...
78
  EXPORT_SYMBOL(of_i2c_register_devices);
138decf83   Adrian Bunk   [POWERPC] drivers...
79

2526c151c   Jon Smirl   drivers/of: Add t...
80
81
  static int of_dev_node_match(struct device *dev, void *data)
  {
61c7a080a   Grant Likely   of: Always use 's...
82
          return dev->of_node == data;
2526c151c   Jon Smirl   drivers/of: Add t...
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  }
  
  /* must call put_device() when done with returned i2c_client device */
  struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
  {
  	struct device *dev;
  
  	dev = bus_find_device(&i2c_bus_type, NULL, node,
  					 of_dev_node_match);
  	if (!dev)
  		return NULL;
  
  	return to_i2c_client(dev);
  }
  EXPORT_SYMBOL(of_find_i2c_device_by_node);
138decf83   Adrian Bunk   [POWERPC] drivers...
98
  MODULE_LICENSE("GPL");