How to use spi device from another kernel module?

Ran Shalit ranshalit at gmail.com
Wed Dec 7 12:26:24 EST 2016


On Wed, Dec 7, 2016 at 6:58 PM, Joel Fernandes <agnel.joel at gmail.com> wrote:
> On Tue, Dec 6, 2016 at 11:02 PM, Greg KH <greg at kroah.com> wrote:
>> On Tue, Dec 06, 2016 at 09:12:24PM -0800, Joel Fernandes wrote:
>>> On Tue, Dec 6, 2016 at 11:42 AM, Ran Shalit <ranshalit at gmail.com> wrote:
>>> > Hello,
>>> >
>>> > I have spi device which is registered using spi_register_board_info(),
>>> > and I would like to get a pointer to this device in some other kernel module.
>>> >
>>> > Is there a simple way to get a pointer to pointer to a device , so
>>> > that we can use it from other module ? (something like i2c_get_adapter
>>> > for i2c)
>>>
>>> Find out what's the SPI bus number (for the master) and the chip
>>> select on that SPI master (for the SPI device)
>>>
>>> Then you can use bus_for_each_device on spi_bus_type and find the
>>> spi_device you're looking for. See the following code for an example
>>> of how to use bus_for_each_device:
>>>
>>> http://lxr.free-electrons.com/source/drivers/spi/spi.c#L524
>>>
>>> In your check function, just make sure your spi->master->bus_num is
>>> the bus you want and the spi->chip_select is the chip select
>>> corresponding to the device you want. If both these conditions are
>>> satisfied, there you have your spi_device.
>>
>> Eeek, no, please never do that, use the proper spi apis to get your
>> needed device.  They are there somewhere, using a "raw"
>> bus_for_each_device is never the answer unless you are a bus and
>> iterating over your own device list.
>
> Yes I completely agree, maybe I assumed too much and thought he was
> trying to do this for some quick debugging from some kernel module,
> and was just looking for a quick and dirty way to get to a spi_device.
> Was not really suggesting this for production code. :)
>

You were actually Right... That's exactly what I wanted :) , so I
probably will  try to use your simple method.
I think that for production , spidev shows a good example how to use
spi as a char device:
https://github.com/Xilinx/linux-xlnx/blob/master/drivers/spi/spidev.c

Thanks!
Ran

> Regards,
> Joel
>
>>
>> thanks,
>>
>> greg k-h



More information about the Kernelnewbies mailing list