kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* How to use spi device from another kernel module?
@ 2016-12-06 19:42 Ran Shalit
  2016-12-07  5:12 ` Joel Fernandes
  0 siblings, 1 reply; 8+ messages in thread
From: Ran Shalit @ 2016-12-06 19:42 UTC (permalink / raw)
  To: kernelnewbies

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)

Regards,
Ran

^ permalink raw reply	[flat|nested] 8+ messages in thread

* How to use spi device from another kernel module?
  2016-12-06 19:42 How to use spi device from another kernel module? Ran Shalit
@ 2016-12-07  5:12 ` Joel Fernandes
  2016-12-07  7:02   ` Greg KH
  0 siblings, 1 reply; 8+ messages in thread
From: Joel Fernandes @ 2016-12-07  5:12 UTC (permalink / raw)
  To: kernelnewbies

On Tue, Dec 6, 2016 at 11:42 AM, Ran Shalit <ranshalit@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.

HTH,

-Joel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* How to use spi device from another kernel module?
  2016-12-07  5:12 ` Joel Fernandes
@ 2016-12-07  7:02   ` Greg KH
  2016-12-07 15:27     ` Ran Shalit
  2016-12-07 16:58     ` Joel Fernandes
  0 siblings, 2 replies; 8+ messages in thread
From: Greg KH @ 2016-12-07  7:02 UTC (permalink / raw)
  To: kernelnewbies

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@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.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 8+ messages in thread

* How to use spi device from another kernel module?
  2016-12-07  7:02   ` Greg KH
@ 2016-12-07 15:27     ` Ran Shalit
  2016-12-07 16:58     ` Joel Fernandes
  1 sibling, 0 replies; 8+ messages in thread
From: Ran Shalit @ 2016-12-07 15:27 UTC (permalink / raw)
  To: kernelnewbies

On Wed, Dec 7, 2016 at 9:02 AM, Greg KH <greg@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@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
>>

Thanks Joel,
Though in this example I can't see how to used bus number & cs.
Probably I ask for something which is not too proper, as Greg mentioned.
Yet, I need to do that, as simple as possible...

>> 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.
>
> thanks,
>
> greg k-h

Hi,

I'm trying to understand how to get the spi device from proc entry.
Is there any example which demonstrate such usage ?

Thanks,
Ran

^ permalink raw reply	[flat|nested] 8+ messages in thread

* How to use spi device from another kernel module?
  2016-12-07  7:02   ` Greg KH
  2016-12-07 15:27     ` Ran Shalit
@ 2016-12-07 16:58     ` Joel Fernandes
  2016-12-07 17:26       ` Ran Shalit
  1 sibling, 1 reply; 8+ messages in thread
From: Joel Fernandes @ 2016-12-07 16:58 UTC (permalink / raw)
  To: kernelnewbies

On Tue, Dec 6, 2016 at 11:02 PM, Greg KH <greg@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@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. :)

Regards,
Joel

>
> thanks,
>
> greg k-h

^ permalink raw reply	[flat|nested] 8+ messages in thread

* How to use spi device from another kernel module?
  2016-12-07 16:58     ` Joel Fernandes
@ 2016-12-07 17:26       ` Ran Shalit
  2016-12-07 18:21         ` Ran Shalit
  0 siblings, 1 reply; 8+ messages in thread
From: Ran Shalit @ 2016-12-07 17:26 UTC (permalink / raw)
  To: kernelnewbies

On Wed, Dec 7, 2016 at 6:58 PM, Joel Fernandes <agnel.joel@gmail.com> wrote:
> On Tue, Dec 6, 2016 at 11:02 PM, Greg KH <greg@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@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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* How to use spi device from another kernel module?
  2016-12-07 17:26       ` Ran Shalit
@ 2016-12-07 18:21         ` Ran Shalit
  2016-12-08 10:42           ` Ran Shalit
  0 siblings, 1 reply; 8+ messages in thread
From: Ran Shalit @ 2016-12-07 18:21 UTC (permalink / raw)
  To: kernelnewbies

On Wed, Dec 7, 2016 at 7:26 PM, Ran Shalit <ranshalit@gmail.com> wrote:
> On Wed, Dec 7, 2016 at 6:58 PM, Joel Fernandes <agnel.joel@gmail.com> wrote:
>> On Tue, Dec 6, 2016 at 11:02 PM, Greg KH <greg@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@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:

Hi,

I did try using this method with the following call:

bus_for_each_dev(&spi_bus_type, NULL, NULL, spi_device_found);

static int spi_device_found(struct device *dev, void *data)
{
    struct spi_device *spi = container_of(dev, struct spi_device, dev);

    printk(":      %s %s %dkHz %d bits mode=0x%02X\n",
        spi->modalias, dev_name(dev), spi->max_speed_hz/1000,
        spi->bits_per_word, spi->mode);

    return 0;
}

But for some reason the list is empty, although I do have devices
registered successfuly with spi_register_driver.
Using the same method with i2c works well, not sure why it doesn't
list anything with spi.

Thanks,
Ran

>>>>
>>>> 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* How to use spi device from another kernel module?
  2016-12-07 18:21         ` Ran Shalit
@ 2016-12-08 10:42           ` Ran Shalit
  0 siblings, 0 replies; 8+ messages in thread
From: Ran Shalit @ 2016-12-08 10:42 UTC (permalink / raw)
  To: kernelnewbies

On Wed, Dec 7, 2016 at 8:21 PM, Ran Shalit <ranshalit@gmail.com> wrote:
> On Wed, Dec 7, 2016 at 7:26 PM, Ran Shalit <ranshalit@gmail.com> wrote:
>> On Wed, Dec 7, 2016 at 6:58 PM, Joel Fernandes <agnel.joel@gmail.com> wrote:
>>> On Tue, Dec 6, 2016 at 11:02 PM, Greg KH <greg@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@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:
>
> Hi,
>
> I did try using this method with the following call:
>
> bus_for_each_dev(&spi_bus_type, NULL, NULL, spi_device_found);
>
> static int spi_device_found(struct device *dev, void *data)
> {
>     struct spi_device *spi = container_of(dev, struct spi_device, dev);
>
>     printk(":      %s %s %dkHz %d bits mode=0x%02X\n",
>         spi->modalias, dev_name(dev), spi->max_speed_hz/1000,
>         spi->bits_per_word, spi->mode);
>
>     return 0;
> }
>
> But for some reason the list is empty, although I do have devices
> registered successfuly with spi_register_driver.
> Using the same method with i2c works well, not sure why it doesn't
> list anything with spi.
>
> Thanks,
> Ran
>
>>>>>
>>>>> 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. :)
>>>
If I may please ask one more thing on this issue, just for my understanding.
Before trying the above suggestions, I made the following trial (which
I know is quick and dirty ):

I copied the pointer to spi device from probe() function into static
variable and than tried to use it later (in proc/sysfs), but I get
exceptions.
I am not going to use this method anyway, yet I am curious why it doesn't work.

Thank you,
Ran

>>
>> 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2016-12-08 10:42 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-12-06 19:42 How to use spi device from another kernel module? Ran Shalit
2016-12-07  5:12 ` Joel Fernandes
2016-12-07  7:02   ` Greg KH
2016-12-07 15:27     ` Ran Shalit
2016-12-07 16:58     ` Joel Fernandes
2016-12-07 17:26       ` Ran Shalit
2016-12-07 18:21         ` Ran Shalit
2016-12-08 10:42           ` Ran Shalit

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).